@ user2166613は正しいですが、少し短いです。これがその方法です。
after()トリガーを使用する例を示します。これは非常に興味深いユースケースです。時間のかかるタスクをWebアプリの呼び出しなどから切り離すことができるため、呼び出しはすぐに制御を返し、処理はバックグラウンドで実行されます。
私の例では、この実行が遅れる関数でシートの列幅を調整します。
// call this function to set a time based trigger and transfer parameters
function setTimeTrigger_AdaptColumnWidths() {
var ssId = SpreadsheetApp.getActiveSpreadsheet().getId();
var wsId = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getSheetId();
var scriptProperties = PropertiesService.getScriptProperties();
scriptProperties.setProperty('spreadsheetId', ssId);
scriptProperties.setProperty('worksheetId', wsId);
// Delay 10 secs, but real execution time may vary up to 15 min!
ScriptApp.newTrigger('adaptColumnWidths').timeBased().after(10000).create();
}
// this function is called by the trigger
function adaptColumnWidths() {
var scriptProperties = PropertiesService.getScriptProperties();
ssId = scriptProperties.getProperty('spreadsheetId');
wsId = scriptProperties.getProperty('worksheetId');
// getSheetById() is a custom function – see below – not yet in Spreadsheet Class!
sheet = getSheetById(SpreadsheetApp.openById(ssId), wsId);
// now do what we want to do in the timeBased trigger
for (var i = 1; i <= sheet.getLastColumn(); i++){
sheet.autoResizeColumn(i);
}
}
// -----
// custom function – hopefully this will become a method of Spreadsheet Class any time soon
function getSheetById(ss, wsId) {
var sheets = ss.getSheets();
for (var i=0; i<sheets.length; i++) {
if (sheets[i].getSheetId() == wsId) return sheets[i];
}
}
ここでは、すべての関数呼び出しに共通のデータスペースに格納していることに注意してください。したがって、短時間に複数の呼び出しを行うと、互いのパラメータが上書きされる可能性があります。
私のユースケースでは、スプレッドシートとワークシートは変更されないため、これは問題ではありません。ただし、通話ごとに変わる可能性のあるデータを転送しようとしないでください。
実際の実行の瞬間は最大15分まで変化する可能性があるため(正確な時間に関係なく)、複数の関数呼び出しが互いに干渉する余地が十分にあります!!!