Highest performance
If you're fine with your script having permission to "See, edit, create, and delete all your Google Sheets spreadsheets", there's a much simpler and faster way to update a set of cells, than any other answers have mentioned so far.
The Advanced Sheets Service lets you write to multiple disjointed ranges at once, using the batchUpdate method and the operation is very fast (compared to .setValues
/ .setFormulas
):
function recalculateCells(recalcData) {
const spreadsheetId = SpreadsheetApp.getActiveSpreadsheet().getId();
// Effectively, quote the values/formulas - https://developers.google.com/sheets/api/reference/rest/v4/ValueInputOption
Sheets.Spreadsheets.Values.batchUpdate({
data: recalcData,
valueInputOption: 'RAW',
}, spreadsheetId);
// Restore them as entered by the user - https://developers.google.com/sheets/api/reference/rest/v4/ValueInputOption
Sheets.Spreadsheets.Values.batchUpdate({
data: recalcData,
valueInputOption: 'USER_ENTERED',
}, spreadsheetId);
}
recalculationData
is an array of objects, where each object has two fields of interest:
range
- the cell to update, in A1 notation, e.g. 'My Custom Sheet'!B2:B2
values
- the value to write, which should be the formula in the cell that you want updated (expressed as a bidimensional array with one row and one column, e.g. [[cellFormula]]
).
You may notice that the spreadsheetId is superflous if the script is run for the current spreadsheet. However, batchUpdate
required the spreadsheet ID because it can be run on any spreadsheet - that's why your script will need permission to "See, edit, create, and delete all your Google Sheets spreadsheets". Please vote for this issue to enable the Advanced Sheets Service to be scoped to only the current spreadsheet.
Slightly lower performance
If you can't obtain the permission to "See, edit, create, and delete all your Google Sheets spreadsheets", you can use a RangeList to store the cells you want to refresh, then .setValues()
, flush(), and finally
.setFormulas()and
flush()` again to force their recalculation.
const activeSS = SpreadsheetApp.getActiveSpreadsheet();
const rangesInSheet = [];
for (const range of sheet.getActiveRangeList().getRanges()) {
const formulas = range.getFormulas();
// Prepare a set of values to store in the cell briefly until
// storing the formulas again. This serves two purposes:
// 1. Provide visual feedback to the user
// 2. More importantly, if the script gets terminated (e.g. due to
// Apps Scripts limits), the original formula is preserved as a value
const recalcValues = [];
for (let row = 0; row < formulas.length; row++) {
recalcValues[row] = [];
for (let col = 0; col < formulas[row].length; col++)
recalcValues[row][col] = `[⚠ RECALCULATING ⚠] ` + formulas[row][col];
}
rangesInSheet.push({
range,
formulas,
recalcValues
});
}
// Flush all recalculation values first, THEN all formulas. This reduces the number of flush() calls from N*2 to 2.
for (const r of rangesInSheet) {
// Storing noop formulas like =NOOP(...) or IFERROR(1/0, ...) didn't work because Sheets apparently caches the
// result of the original formula and returns it right away to the noop. The only way to trigger calling the
// custom functions again was to set the *values* of the cells, then setFormulas() again.
console.log(`Storing raw values for ${r.range.getA1Notation()}...`); // won't display the sheet name
r.range.setValues(r.recalcValues);
}
SpreadsheetApp.flush(); // force changes to be written
for (const r of rangesInSheet) {
console.log(`Restoring original formulas for ${r.range.getA1Notation()}...`); // won't display the sheet name
r.range.setFormulas(r.formulas);
console.log('Formulas recalculated.');
}
// Force restoring original formulas; both flush()es are necessary
SpreadsheetApp.flush();
I've benchmarked this approach to 50+ cells per second, but haven't tried larger numbers of cell. For reasonable numbers of cells, it may be practically as fast as the batchUpdate()
approach.