Not able to pass parameters from cloud function to google apps script and return a response

457 views
Skip to first unread message

Krithika Muthukrishnan

unread,
Sep 21, 2020, 12:38:05 AM9/21/20
to Google Apps Script Community

I am calling a google app script from my firebase cloud function as shown below. I am able to call the script successfully and create a google form, but am not able to a. send a parameter/ data from the cloud function to the google app script when calling the script.run b. get the right data (url) for the created form back from the google app script in the response to the cloud function.

I am new to app script. Please help me understand what I am doing wrong.


My appscript code:


function doPost(e) {
    var postJSON = e.parameter; // e is undefined eventhough data is being passed
    console.log("postJSON is: "+ JSON.stringify(postJSON));
    doGet();
    }

    function doGet(e) {  
    // create & name Form  
    var item = "Sample Form_SMT";  
    var form = FormApp.create(item)  
   .setTitle(item);  

   // single line text field  
    ... some code to create the google form

   // the form url is correctly logged.
    var url = form.getEditUrl();
    console.log("url of the form is " + URL);

  // id of the form is correctly logged
    var formId = form.getId();
    console.log("the id of the form is " + formId);

    const result = {'url': url};
    var JSONString = JSON.stringify(result);
    var JSONOutput = ContentService.createTextOutput(JSONString);
    JSONOutput.setMimeType(ContentService.MimeType.JSON);

    return JSONOutput; // this output is NOT being returned to the cloud function
}




My cloud function code:


import * as functions from "firebase-functions";
const fs = require("fs");
const { google } = require("googleapis");
const googleAuth = require("google-auth-library");
const script = google.script("v1");

const scriptId = "MY_SCRIPT_ID";
    // calling the cloud function from my javascript app
export const gpublish = functions.https.onCall((data: any, response: any) => {
const test = data.test;
return new Promise((resolve, reject) => {
// Authenticating with google app script
fs.readFile("gapi_credentials.json", (err: any, content: string) => {
    const credentials = JSON.parse(content);
    const { client_secret, client_id, redirect_uris } = credentials.web;
    const functionsOauth2Client = new googleAuth.OAuth2Client(client_id, client_secret,redirect_uris);
    functionsOauth2Client.setCredentials({refresh_token: credentials.refresh_token});

    // call the google app script
return runScript(functionsOauth2Client, scriptId, test.testName)
      .then((scriptData: any) => {
        console.log("returned script response is " + JSON.stringify(scriptData));
      })
      .catch((err4) => {
        console.log("There is some problem with the script running ");
        return 'ERROR_RESPONSE';
      });
}); }); });

  function runScript(auth: any, scriptid: string, testName: string) {
    return new Promise(function (resolve, reject) {
     script.scripts.run(
  {
    auth: auth,
    scriptId: scriptid,
    resource: {
      function: "doPost",
      parameters: testName,
    },
  },
  function (err3: any, respons: any) {
    if (err3) {
      console.log("API returned an error: " + err3);
      reject(err3);
    }
    else {
      console.log(" the script is run and response is " + JSON.stringify(respons));
      resolve(respons);
    }
   });
 });
 }


The data is not received in my app script and in doPost I get this error.
ReferenceError: e is not defined



Am I passing the data correctly from the cloud function scripts.run? 
(I have followed this link: https://developers.google.com/apps-script/api/how-tos/execute)
Am I receiving the data correctly in my app script?
Please help me resolve this error.


cbmserv...@gmail.com

unread,
Sep 21, 2020, 1:16:49 AM9/21/20
to google-apps-sc...@googlegroups.com

Your bug is that you are calling doGet from doPost but not passing the e parameter in the call.

 

So e is undefined as it is not passed in.

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/f5543255-18f6-4c25-8e38-3a43d81e1801o%40googlegroups.com.

Krithika Muthukrishnan

unread,
Sep 21, 2020, 6:18:44 AM9/21/20
to google-apps-sc...@googlegroups.com
Thank you, While your suggestion helped, I needed to do more - this answer helped me get the data from the cloud function to the appscript. 

However, Iam still not able to get the JSONoutput sent back from the app script in the response received by the cloud function. Can you please help me with that? The response Iam getting in the cloud function is below.

{
    "config": {
        "method": "POST",
        "data": {
            "function": "doPost",
            "parameters": "{\"testName\":\"test - 1\""
        },
        "headers": {
            "Accept-Encoding": "gzip",
            "User-Agent": "google-api-nodejs-client/0.7.2 (gzip)",
            "Authorization": "Bearer Some auth info",
            "x-goog-api-client": "gl-node/12.18.2 auth/6.0.6",
            "Content-Type": "application/json",
            "Accept": "application/json"
        },
        "params": {},
        "body": "{\"function\":\"doPost\",\"parameters\":\"{\\\"testName\\\":{\\\"test-1\\\"}",
        "responseType": "json"
    },
    "data": {
        "done": true,
        "response": {
        }
    },
    "headers": {
        "alt-svc": "h3..."",
        "cache-control": "private",
        "connection": "close",
        "content-encoding": "gzip",
        "content-type": "application/json; charset=UTF-8",
        "date": "Mon, 21 Sep 2020 10:03:55 GMT",
        "server": "ESF",
        "transfer-encoding": "chunked",
        "vary": "Origin, X-Origin, Referer",
        "x-content-type-options": "nosniff",
        "x-frame-options": "SAMEORIGIN",
        "x-xss-protection": "0"
    },
    "status": 200,
    "statusText": "OK",
    "request": {
    }
}



To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-community+unsub...@googlegroups.com.

CBM Services

unread,
Sep 21, 2020, 1:25:44 PM9/21/20
to google-apps-sc...@googlegroups.com
I would suggest you decouple the doGet from doPost. Having one call the other is probably messing up the return parks as they are only returns from doGet.

Put the full code in your doPost function instead and then debug further.

From: Krithika Muthukrishnan
Sent: ‎2020-‎09-‎21 3:18 AM
Subject: Re: [Apps-Script] Not able to pass parameters from cloud function togoogle apps script and return a response

Thank you, While your suggestion helped. I needed to do more. - this answer helped me get the data from the cloud function to the appscript. 

However, Iam still not able to get the JSONoutput sent back in the response received by the cloud function. Can you please help me with that? The response Iam getting now is below.

[The entire original message is not included.]
Reply all
Reply to author
Forward
0 new messages