Using the Google Sheets API from Script apps for a single sheet

Using Script applications linked to a spreadsheet to create a worksheet is very straightforward through the global SpreadsheetApp. However, there are some functions, such as receiving / setting filters on a worksheet, which can be accessed only from the Google Sheets REST API.

I saw an example that uses UrlFetchApp from Script applications to invoke the Google Sheet API, but it is written as if Script applications are not associated with a specific table.

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 try to call this function from table-related Script applications, I get the error message "Request has not authentication scopes".

Script , , ScriptApp.getOAuthToken(), , .

.

+4
1

-, , API .

"Request had insufficient authentication scopes" - OAuth 2.0, , , .

, , OAuth 2.0, .

.

ScriptApp.getOAuthToken() thread

0

Source: https://habr.com/ru/post/1656614/


All Articles