繼續我之前的
這是繞過它的代碼片段:
function CheckPageSpeed(url) {
const apiKey = "###"; // Please set your API key.
const apiEndpoint = `https://www.googleapis.com/pagespeedonline/v5/runPagespeed?key=${apiKey}&url=${encodeURIComponent(url)}&category=performance`;
const strategy = ["mobile"];
const res = UrlFetchApp.fetchAll(strategy.map(e => ({ url: `${apiEndpoint}&strategy=${e}`, muteHttpExceptions: true })));
const values = res.reduce((o, r, i) => {
if (r.getResponseCode() == 200) {
const obj = JSON.parse(r.getContentText());
o[strategy[i]] = obj.lighthouseResult.categories.performance.score * 100;
} else {
o[strategy[i]] = null;
}
return o;
}, {});
return values.mobile;
}
當我在 中使用它時Google sheets as custom formula,有時需要很長時間才能使作業表引發以下錯誤:

有什么方法可以解決這個錯誤,讓它重新開始計算分數而不是拋出錯誤?謝謝你。
uj5u.com熱心網友回復:
問題和解決方法:
從您的顯示影像、您的錯誤Exceed maximum execution time和更新的腳本來看,在這種情況下,認為腳本的執行時間超過 30 秒。(在當前階段,自定義函式的最大執行時間為 30 秒。Ref)在這種情況下,當錯誤Exceed maximum execution time發生時,很遺憾,這不能用作觸發器。而且,在當前階段,UrlFetchApp不能隨著時間的推移而停止。而且,例如,即使檢索到所有 URL 并從 API 檢索每個值,我也不確定處理時間是否超過 6 分鐘。我很擔心這個。
從上述情況來看,僅手動重新運行出現錯誤的自定義函式怎么樣?
示例腳本:
在使用此腳本之前,請在 Advanced Google services 中啟用 Sheets API。通過電子表格上的按鈕和/或自定義選單執行此功能怎么樣?
function reCalculation() {
const sheetName = "Sheet1"; // Please set sheet name.
const formula = "=CheckPageSpeed"; // Please set the function name of your custom function.
const dummy = "=sample";
const ss = SpreadsheetApp.getActiveSpreadsheet();
const ssId = ss.getId();
const sheet = ss.getSheetByName(sheetName);
const sheetId = sheet.getSheetId();
const values = sheet.getRange("B1:B" sheet.getLastRow()).getDisplayValues();
const requests = values.reduce((ar, [a], i) => {
if (a == "#ERROR!") {
ar.push({ findReplace: { range: { sheetId, startRowIndex: i, endRowIndex: i 1, startColumnIndex: 1, endColumnIndex: 2 }, find: `^${formula}`, replacement: dummy, includeFormulas: true, searchByRegex: true } }); // Modified
}
return ar;
}, []);
if (requests.length == 0) return;
Sheets.Spreadsheets.batchUpdate({ requests }, ssId);
SpreadsheetApp.flush();
requests.forEach(r => {
r.findReplace.find = dummy;
r.findReplace.replacement = formula;
r.findReplace.searchByRegex = false;
});
Sheets.Spreadsheets.batchUpdate({ requests }, ssId);
}
- 運行此腳本時,僅
#ERROR!重新計算“B”列中的單元格。
筆記:
- 我想在這種情況下,這個函式也許可以由時間驅動的觸發器來執行。但是,在這種情況下,它可能會影響時間驅動觸發器的配額(最大執行時間為 90 分鐘/天)。因此,在這個答案中,我建議使用手動操作來運行此功能。
參考:
- 方法:電子表格.batchUpdate
- 查找替換請求
添加:
例如,在您的情況下,使用fetchAll方法直接請求 API 端點怎么樣?示例腳本如下。在這種情況下,將從“A”列檢索 URL,并檢索值并將其放入示例電子表格中的“C”列。
示例腳本:
請設定您的 API 密鑰。并且,請使用腳本編輯器運行此腳本。通過這種方式,使用 API 檢索值。
function reCalculation2() {
const apiKey = "###"; // Please set your API key.
const sheetName = "Sheet1"; // Please set sheet name.
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getSheetByName(sheetName);
const values = sheet.getRange("A2:A" sheet.getLastRow()).getValues();
const requests = values.map(([url]) => {
const apiEndpoint = `https://www.googleapis.com/pagespeedonline/v5/runPagespeed?key=${apiKey}&url=${encodeURIComponent(url)}&category=performance&strategy=mobile`;
return { url: apiEndpoint, muteHttpExceptions: true };
});
const res = UrlFetchApp.fetchAll(requests);
const v = res.map(r => {
if (r.getResponseCode() == 200) {
const obj = JSON.parse(r.getContentText());
return [obj.lighthouseResult.categories.performance.score * 100];
}
return [null];
});
sheet.getRange(2, 3, v.length).setValues(v);
}
- 在這種情況下,
fetchAll使用方法。通過這個,我認為Exceeded maximum execution可能可以避免錯誤。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/496008.html
