I'm trying to run a script in Google Sheets and fail under following circumstances:
- I'm logged in into my own Google account,
- Open script editor,
- paste script, save and run,
- get an alert to authorize and
- finally get an error
Sign in with Google temporarily disabled for this app. This app has not yet been verified by Google in order to use Google Sign in.
Q: What do i do wrong? How can i run a script in my own Google Sheet? I don't plan to share anything of sheet or script access - its only for my access.
Script details: script gets Web Vitals metrics for given URL. The key
(line 45) is PageSpeed API key, created in the Google Developer Console, owned by me and scoped to this Google Account.
PS: no solution from "Sign in with Google temporarily disabled for this app" error when trying to authorize a script worked for me
PPS: here is the script body:
function getUrls() {
const data = SpreadsheetApp.getActiveSpreadsheet()
.getSheetByName("URLs")
.getDataRange()
.getDisplayValues()
.map(([url, name]) => ({ url, name }))
.filter(({ url }) => /^http/i.test(url));
return data;
}
function parseResponse(metrics, audits, strategy) {
function getPercentile(key) {
return metrics[key].percentile;
}
function getValue(key) {
return audits[key].numericValue.toFixed(1);
}
const key = (text) => `${text} (${strategy})`;
return {
[key("First Contentful Paint")]: getPercentile("FIRST_CONTENTFUL_PAINT_MS"),
[key("First Input Delay")]: getPercentile("FIRST_INPUT_DELAY_MS"),
[key("Largest Contentful Paint")]: getPercentile(
"LARGEST_CONTENTFUL_PAINT_MS"
),
[key("Cumulative Layout Shift")]: getPercentile(
"CUMULATIVE_LAYOUT_SHIFT_SCORE"
),
[key("Speed Index")]: getValue("speed-index"),
[key("Time to Interactive")]: getValue("interactive"),
[key("Estimated Input Latency")]: getValue("estimated-input-latency"),
};
}
function fetchPage(url, strategy) {
try {
toast(`Processing ${url} (${strategy})`);
const params = {
url,
strategy,
category: "performance",
fields: "loadingExperience,lighthouseResult(audits)",
key: "AIzaSyCTn2nzpXRX8POlvnWoozTSXf2NvrzqAE0",
};
const qs = Object.keys(params)
.map((key) => `${key}=${params[key]}`)
.join("&");
const apiUrl = `https://www.googleapis.com/pagespeedonline/v5/runPagespeed?${qs}`;
let response = null;
try {
response = UrlFetchApp.fetch(apiUrl, {
muteHttpExceptions: false,
});
} catch (f) {
Utilities.sleep(5000);
response = UrlFetchApp.fetch(apiUrl, {
muteHttpExceptions: true,
});
}
const {
error,
loadingExperience: { metrics = null } = {},
lighthouseResult: { audits = null } = {},
} = JSON.parse(response.getContentText());
if (!error) return parseResponse(metrics, audits, strategy);
} catch (f) {}
return null;
}
function writeDataToSheet(name, data) {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet =
ss.getSheetByName(name) ||
ss.insertSheet(name, 1, { template: ss.getSheetByName("template") });
const vitals = sheet
.getRange(2, 1, sheet.getLastRow() - 1, 1)
.getValues()
.map(([e]) => {
return [data[e]];
});
sheet
.getRange(1, sheet.getLastColumn() + 1, vitals.length + 1, 1)
.setValues([[new Date()], ...vitals]);
}
function toast(e) {
try {
SpreadsheetApp.getActiveSpreadsheet().toast(e);
} catch (f) {}
}
function coreWebVitals() {
const urls = getUrls();
for (let u = 0; u < urls.length; u++) {
const { name, url } = urls[u];
const data = { ...fetchPage(url, "desktop"), ...fetchPage(url, "mobile") };
if (data !== null) {
writeDataToSheet(name, data);
}
}
SpreadsheetApp.flush();
}
function init() {
ScriptApp.getProjectTriggers().forEach((trigger) =>
ScriptApp.deleteTrigger(trigger)
);
ScriptApp.newTrigger("coreWebVitals").timeBased().everyDays(1).create();
toast("Working!");
coreWebVitals();
toast("Success!");
Browser.msgBox(
"You're all set. The metrics will be updated once per day automatically"
);
}