Abiding by best practices, you should minimize repeated calls to the Spreadsheet Service through the use of getValues()
. Your current script loops accesses the values of A7:A2999 one-by-one, when it could do it in a single read:
// const x -> no reassignment / redeclaring x allowed. (You can still manipulate the object).
const startRow = 7, endRow = 2999,
numRows = endRow - startRow + 1;
const months = ss.getSheetByName(location)
.getRange(startRow, 1, numRows, 1).getValues();
Then you conditionally access columns B:D of that same range. You will find that it is still faster to simply read this all into memory at the start, and only access your desired rows & columns when needed:
const startCol = 2, endCol = 4,
numCols = endCol - startCol + 1;
const targetValues = ss.getSheetByName(location)
.getRange(startRow, startCol, numRows, numCols).getValues();
You should also use more meaningful iteration index names than i
and j
, and you don't have declare everything at the start of your function (read about JavaScript and 'hoisting'), when point-of-use is more meaningful.
The rest of your function then looks like this:
const output = [],
targetMonth = /** set this */,
destinationName = /** set this */;
for (var monthIndex = 0; monthIndex < months.length; ++monthIndex) {
// Add the array of target column values to the output (by reference)
if (months[monthIndex][0] === targetMonth) {
output.push(targetValues[monthIndex]);
}
}
// Write the output array.
ss.getSheetByName(destinationName).getRange(8, 3, output.length, output[0].length).setValues(output);
We just went from ~numRows x numColumns Spreadsheet accesses to just ~4! Given that individual cell access calls takes around 0.1 seconds this will be boatloads faster. Yes, if you read a lot of cells it can take a while (getting/setting values from 400k cells is ~30 seconds), but it takes nowhere near as long as 1-by-1 access.
References:
PS: if you change the values in targetValues
before you serialize the data, those referenced in output
will update as well, because they are the same object. (Read about "by value" / deep copy and "by reference" / shallow copy to understand why. For this script as written, the distinction does not matter because they are not modified.)