Using urlFetchApp with 0Auth when calling function from google sheets

37 views Asked by At

Hi dear Fellow Stack Overflowers,

I am working on passing variables between two different google sheets the difficult way: Using urlFetchApp, doPost() and 0Auth. I have got really far after hours of research and a lot of trial and error because I managed to make it work when I run the script with the debugger. However, when I call the function from Google Sheets it gives me an error which I am unable to fix.

Would anyone of you be able to accept and overcome this challenge?

Here is the code for the google sheet that requests the value of the Cell B2 of the other google sheet:

function rowcolurl() {
//Needs to be deployed with access to anyone to be able to urlfetch it with the other google sheets
// Or authenticated following the oauth method in https://barbersmith.com/posts/oauth-in-google-apps-script/
// See more in https://developers.google.com/identity/protocols/oauth2/scopes

//var url=SpreadsheetApp.getActive().getRange("F2").getValues();
//var currRow = SpreadsheetApp.getActiveSheet().getActiveCell().getRow();
//var currCol = SpreadsheetApp.getActiveSheet().getActiveCell().getColumn();

var currRow=1;
var currCol=2;

var passedrowcol = {"passedrow" : currRow,"passedcol" : currCol};

var url = 'https://script.google.com/macros/s/AKfycbzlpE7x39Ygtnlp8I_nKyOmbQ00HsUjEfezKOFw6qjMSzjjhVz_PT9bfoQteePm6qNw/exec';

var options = {
  'method' : 'post',
  'contentType': 'application/json',
  // Convert the JavaScript object to a JSON string.
  'payload' : JSON.stringify(passedrowcol),
  'muteHttpExceptions': true,
  'headers' :  { 'Authorization' : "Bearer "+ ScriptApp.getOAuthToken()}
};

var response = UrlFetchApp.fetch(url,options);
Logger.log(response);

var content = response.getContentText();
var json = JSON.parse(content);
var responseCellContent = json["Cellcontent"];
Logger.log(responseCellContent);

  return responseCellContent;
}

To get this to work in the debugger you need to tick the box "Show "appsscript.json" manifest file in editor" on the project settings. You also need to edit appsscript.json to say:

{
  "timeZone": "Europe/London",
  "dependencies": {  },
  "oauthScopes": [
    "https://www.googleapis.com/auth/script.external_request",
    "https://www.googleapis.com/auth/devstorage.read_only",
    "https://www.googleapis.com/auth/spreadsheets",
    "https://www.googleapis.com/auth/drive"],
  "exceptionLogging": "STACKDRIVER",
  "runtimeVersion": "V8"
}

On the Google sheet that contains the data the code is the following:

function doPost(e) {
  
var passedrow = JSON.parse(e.postData.contents).passedrow;
var passedcol = JSON.parse(e.postData.contents).passedcol;

var celldata = SpreadsheetApp.getActive().getSheets()[0].getRange(passedrow,passedcol).getValues();

var celldatajson = {"Cellcontent" : celldata};

var JSONString = JSON.stringify(celldatajson);

  return ContentService.createTextOutput(JSONString).setMimeType(ContentService.MimeType.JSON);
};

The second Google sheet should return the value of the cell "B2" to the first sheet using doPost, urlFetchApp with authentication which means deploying the doPost script to only "Allow me" to use it in a secure way. And it works when I click the button play or use the Debugger in the App Script Editor but not when I call it from a Cell in Google Sheets.

0

There are 0 answers