Using an Apps Script that's bound to a Spreadsheet to effect the sheet is very straight forward, via the SpreadsheetApp global. However, there's some functionality, such as getting/setting filters on a sheet, that can only be accessed from the Google Sheets REST API.
I've seen an example that uses UrlFetchApp from within an Apps Script, to call the Google Sheet API, but it's written as if the Apps Script is not actually bound to a particular Spreadsheet.
function getSheetBasicFilter(ssId, sheetId) {
var params = {
headers: {
"Authorization": "Bearer " + ScriptApp.getOAuthToken()
}
};
var base = "https://sheets.googleapis.com/v4/spreadsheets/";
var partialRespParams = "?fields=sheets%2FbasicFilter";
var resp = UrlFetchApp.fetch(base + ssId + partialRespParams, params);
var sheets = JSON.parse(resp.getContentText()).sheets;
for (var i = 0; i < sheets.length; i++) {
if (sheets[i].basicFilter
&& (sheets[i].basicFilter.range.sheetId == sheetId
|| (!sheets[i].basicFilter.range.sheetId && sheetId == 0))) {
return sheets[i].basicFilter;
}
}
return null;
}
When I attempt to call this function from within a Spreadsheet Bound Apps Script, I get an error 'Request had insufficient authentication scopes'.
Since the script is running as the user that owns the document, and ScriptApp.getOAuthToken() is being used, it's not clear why this error is occurring.
Any help would be appreciated.