0

i want to sent get request by using script on google spreadsheet. Trigger of this requset is edit by spreadsheet by user. The idea was next:user change data in spreadsheet1 and script of spreadsheet1 sent request to spreadsheet2. Script of spreadsheet2 have doGet() function and do something. But nothing work.

I make two simple spreadsheets for exp. spreadsheet1 spreadsheet2

My code in spreadsheet1:

function myFunction() {

  var response = UrlFetchApp.fetch("https://script.google.com/d/15x62kDTppUbyUBo7LWKHG-Csw4yOKvxpodSYpYXoRGxky68O7Bx9uigZ");

Logger.log(response.getContentText());
}

In UrlFetchApp.fetch(url) i use url of spreadsheet2 script.

My code in spreadsheet2:

function doGet(e) {

  Logger.log("GET запрос");

}

function doPost(e){

  Logger.log("POST запрос");      
}

I read

link1 link2 link3 link4

but this does not work

1 Answers1

0

It seems you are using the Logger.log function expecting to create a HTML response.

Try using the TextOutput or the HtmlOutput objects to create your text instead.

Something like:

function doGet(e) {
   return ContentService.createTextOutput("GET запрос");
}

function doPost(e){
   return ContentService.createTextOutput("POST запрос");      
}

Hope this helps!

ZektorH
  • 2,680
  • 1
  • 7
  • 20
  • As i understand i need to deploy as a WebApp the google script to send get request to him. But can i sent get request to simple google script in google spreadsheet container ? – Dmitriy Lavrov Oct 08 '19 at 10:50
  • i will grateful to you if you will help :) – Dmitriy Lavrov Oct 08 '19 at 11:28
  • i read about [UrlFetchApp](https://developers.google.com/apps-script/reference/url-fetch#methods_1) where wrote ```fetch(url), HTTPResponse - Makes a request to fetch a URL.``` - but it does not work ( – Dmitriy Lavrov Oct 08 '19 at 11:37
  • Hello @DmitriyLavrov, what happens after you do the fetch? – ZektorH Oct 08 '19 at 11:45
  • Hello @ ZektorH. this is my [script](https://script.google.com/d/MUUhTFZMl3DUYrYG_5jhwgJOmxho0XLdw/edit?mid=ACjPJvH9vRAnJM64cIVHbbes56OLvJA286B_y6LdbidottIhRVg3wqE5JYEDk6HiOFX8yn10MJ3Zz9YfP0rnOYJXrD6p-1GM6W9IIrE2oXvP2MwbtxPWbCL5IcJn4vWRR9aCyNVGTVWJWTY&uiv=2). It is Spreadsheet1. So code is short ```function fun1() { Logger.log(" start"); UrlFetchApp.fetch("URL_WebApp"); }``` – Dmitriy Lavrov Oct 08 '19 at 11:51
  • Happens nothing. Script dont say about fails. – Dmitriy Lavrov Oct 08 '19 at 11:53
  • I think you should read more on the topic. Try following the [script examples](https://sites.google.com/site/scriptsexamples/learn-by-example/parsing-html) and remember that `Logger.log` does not show text on the sheet, it just shows on the [Stackdriver Logs](https://developers.google.com/apps-script/guides/logging) – ZektorH Oct 08 '19 at 11:56
  • i know it. But i dont need to output html content. I just need to call doGet() in script2 from function script1. I can call doGet() in script2 by using copy url WebApp to browser. But i cant call it from script1 by using UrlFetchApp.fetch() – Dmitriy Lavrov Oct 08 '19 at 12:09
  • something similar there are [here](https://stackoverflow.com/questions/25228684/google-app-script-make-get-request-to-another-script-with-parameters/58285970#58285970) , but there are not full code to understend it. – Dmitriy Lavrov Oct 08 '19 at 12:10
  • `UrlFetchApp.fetch(addressOfYourSpreadsheet2WebApp);` is all you need to execute a doGet(e) on spreadsheet 2. – ZektorH Oct 08 '19 at 12:12
  • yes and i do the same. my code of script1 ```function fun1() { UrlFetchApp.fetch("https://script.google.com/macros/s/AKfycbxaMxSYqgJB_bEHLuLEWEyOULna-VblHrTM7DiMmaLmbTEZ6OU/exec"); } ``` and no result. But if i copy url and past it to browser - its work. – Dmitriy Lavrov Oct 08 '19 at 12:28
  • i solve the problem. The problem was in access right. – Dmitriy Lavrov Oct 08 '19 at 12:36
  • In script2 i change the settings - I opened access to all anonymous users from the Internet. Then calling the doget() function of script2 from script1 works. – Dmitriy Lavrov Oct 08 '19 at 12:39
  • Maybe i can turn on authorization, but i dont know how. – Dmitriy Lavrov Oct 08 '19 at 12:45