Reputation: 170
I am trying to write an add on for google spread sheet that takes a csv and write onto a new sheet. The problem is that the csv can potentially be very large, reaching up to around a million rows. I managed to fix the cell limit problem by deleting unnecessary columns, but my script keeps timing out even at 500,000 rows. So I was wondering if there is a way to increase the write speed?
Here is my code where I create a new sheet and populate it with the csv data:
function writeDataToSheet(data,sheetName) {
const ss = SpreadsheetApp.getActive();
const sheet = ss.insertSheet(sheetName);
const neededColumns = data[0].length;
const totalColumns = sheet.getMaxColumns();
const columnsToDelete = totalColumns - neededColumns;
if(columnsToDelete > 0) sheet.deleteColumns(totalColumns , columnsToDelete);
sheet.getRange(1, 1, data.length, data[0].length).setValues(data);
return sheet.getName();
}
To be clear, data is supposed to be a 2 dimensional array, converted from csv data using Utilities.parseCsv()
.
Upvotes: 0
Views: 1133
Reputation: 201358
In my measured benchmark, when the data is large, when Sheets API is used instead of the Spreadsheet service (SpreadsheetApp
), the process cost can be reduced. Ref This situation is almost the same with and without V8 runtime. This has already been mentioned in TheMaster's comment. In your script, when Sheets API is used, it becomes as follows.
Before you use this modified script, please enable Sheets API at Advanced Google services.
In this modification, your data
is used. Please modify your script as follows.
sheet.getRange(1, 1, data.length, data[0].length).setValues(data);
Sheets.Spreadsheets.Values.update({values: data}, ss.getId(), sheet.getSheetName(), {valueInputOption: "USER_ENTERED"});
In this modification, from To be clear, data is supposed to be a 2 dimensional array, converted from csv data using Utilities.parseCsv().
, the CSV data before data
is directly used. Please modify your script as follows. So, please directly put csvData
to the following request.
sheet.getRange(1, 1, data.length, data[0].length).setValues(data);
function writeDataToSheet(csvData, sheetName) {
const ss = SpreadsheetApp.getActive();
const sheet = ss.insertSheet(sheetName);
const requests = { requests: [{ pasteData: { data: csvData, coordinate: { sheetId: sheet.getSheetId() } } }] };
Sheets.Spreadsheets.batchUpdate(requests, ss.getId());
}
Upvotes: 3