您好,我有一個宏記錄,它呼叫 URL 獲取函式,該函式從特定 URL 獲取一些元資料
例如,我在宏中有 12 條記錄,并且希望在每個宏觸發后暫停 2 秒。
function FetchMetaData() {
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.getRange('AV4').activate();
spreadsheet.getCurrentCell().setFormula('=HtmlLang($G$2)');
spreadsheet.getRange('AV6').activate();
spreadsheet.getCurrentCell().setFormula('=MetaCharset($G$2)');
spreadsheet.getRange('AV9').activate();
spreadsheet.getCurrentCell().setFormula('=MetaViewport($G$2)');
spreadsheet.getRange('AV10').activate();
spreadsheet.getCurrentCell().setFormula('=Title($G$2)');
spreadsheet.getRange('AV11').activate();
spreadsheet.getCurrentCell().setFormula('=MetaD($G$2)');
spreadsheet.getRange('AV15').activate();
spreadsheet.getCurrentCell().setFormula('=MetaPropertyLocale($G$2)');
};
我應該在上面的代碼中添加延遲函式 Utilty
Utilities.sleep(500);
我正在嘗試這個,因為整個腳本宏將使用 fecth(url) 函式獲取整個單個域,并且實時檢查的總數將接近 200 個單獨的宏,我已經在電子表格的下拉選單中。一旦我將所有宏記錄在一個宏中,上面的腳本將有 200 條記錄。顯然,他們都在使用 fetch 功能,并且多合一運行會在電子表格上增加大量延遲,并且可能會根據網站擁有的 url 數量增加一些塊。
我已經嘗試添加
Utilities.sleep(500);
below the var function but this is not add a delay between fetch runner like
function FetchMetaData() {
var spreadsheet = SpreadsheetApp.getActive();
Utilities.sleep(500);
Also try this way
function FetchMetaData() {
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.getRange('AV4').activate();
Utilities.sleep(3500);
spreadsheet.getCurrentCell().setFormula('=HtmlLang($G$2)');
spreadsheet.getRange('AV6').activate();
Utilities.sleep(3500);
spreadsheet.getCurrentCell().setFormula('=MetaCharset($G$2)');
};
And this way
function FetchMetaData() {
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.getRange('AV4').activate();
spreadsheet.getCurrentCell().setFormula('=HtmlLang($G$2)');
Utilities.sleep(3500);
spreadsheet.getRange('AV6').activate();
spreadsheet.getCurrentCell().setFormula('=MetaCharset($G$2)');
Utilities.sleep(3500);
};
Also between every .activate value but the macro runn all of the functions without delay
Any idea how can i do this in more elegant way to add the delay with a few rows of code. Should i add all Range in Loop and For Each Function to run the Sleep Delay Or Any Help i will Appreciate Thanks
uj5u.com熱心網友回復:
試試這種方式:
function FetchMetaData() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getActiveSheet();
const rgA = sh.getRangeList(['AV4','AV6','AV9','AV10','AV11','AV15']).getRanges();
const fA = ['=HtmlLang($G$2)','=MetaCharset($G$2)','=MetaViewport($G$2)','=Title($G$2)','=MetaD($G$2)','=MetaPropertyLocale($G$2)'];
rgA.forEach((r,i) => {
r.setFormula(fA[i]);
SpreadsheetApp.flush();
});
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/426843.html
