I've written a short script to copy over certain rows from one sheet to another. The initial sheet is a larger dataset and the second sheet should be a filtered set to create custom graphs on the dataset more easily.
The current code:
function updateChartRows() {
var sheetOriginal = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("sheet1");
var sheetChart = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("sheet2");
var filterValue = "someValue";
var matches = sheetOriginal.createTextFinder(filterValue);
var match = null;
var i = 0;
while ((match = matches.findNext()) != null) {
var search_range = sheetOriginal.getRange(match.getRowIndex(), 1,1,3);
var update_range = sheetChart.getRange(i+3,1,1,3);
update_range.setValues(search_range.getValues());
i++;
}
}
The code is rather simple. It goes to the original sheet and does a find on the data based on a keyword. It gets an array of cells matching the keyword.
I then loop over the matches array and copy the values of the entire row of that cell to a new row on the second sheet. We only need the values from the first three columns. (We can make the assumption te new sheet is empty and we start from the third row due to labeling in the first and second row.)
I notice that the code now has an execution time of roughly 30 seconds. Which I find rather slow since the current original dataset is still on the small side. Roughly 60 records.
I assume the slowness lives in the while loop. For every match it has to do a lookup in the original sheet to find the correct rows and values.
Is there a way to improve on this? For example, instead of creating a textFinder is there better way to get all the relevant data, including the actual values instead of references to the cell?
I also tried the findAll() function on the TextFinder, but I still only get references cell matching the keyword. Thus I still need to do a lookup to get the values from the entire row.