Answer:
You can use a minute trigger to run one query per minute, using PropertiesService to store how far you've gone through the sheet.
More Information:
PropertiesService is a class which allows you to store information within a script so that it can be accessed on each run. This means you can retain information/variables across runs, even after the script has finished execution.
The idea is as such:
- Create a property store which saves the last row that the script ran on
- On each run, check the property store. If there is a last row saved, run the script on the next row, and save the new last row to the property store.
- If the current row is empty, assume we have hit the end of the sheet and delete the property store.
- If there is no property store, start from the beginning of the sheet again
- Run the script once per minute, so each status code retrieval will be staggered
Code Example:
// Copyright 2021 Google LLC.
// SPDX-License-Identifier: Apache-2.0
// Define your sheet so it can be accessed from all functions
const ss = SpreadsheetApp.getActiveSpreadsheet()
// Don't forget to change your sheet name:
const sheet = ss.getSheetByName("Sheet1")
// This is the function to run on minute trigger
function onMinuteTrigger() {
// Define the property service and check if there is a last row saved
const sp = PropertiesService.getScriptProperties()
let row = sp.getProperty("last")
// if there was no last row, set the 'row' variable to 0
if (!row) row = 0
// add 1 to row so the script runs on the next row, not the last
row++
// call the status code function
const statusCode = getStatusCode(row)
// if the statusCode is null then the row was blank;
// so delete the property store and return
if (statusCode == null) {
sp.deleteProperty("last")
return
}
// if the script has got this far there was a returned status code
// save the status code to the sheet and save the new last row
// to the property store
sheet.getRange(row, 2).setValue(statusCode)
sp.setProperty("last", row)
}
Along with a slightly modified version of your statusCode() method:
// modified statusCode function from your question
// pass to it the row to update
function getStatusCode(row) {
const options = {
'muteHttpExceptions': true,
'followRedirects': false
}
// manually get the url from the passed row variable
const url_trimmed = sheet.getRange(row, 1).getValue().trim()
// if url_trimmed is blank then the row is empty, return null
if (url_trimmed.length == 0) return null
// fetch the URL and return the response code
const response = UrlFetchApp.fetch(url_trimmed, options)
return response.getResponseCode()
}
References: