0

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 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);
    }
   });
 });
 }

My google app script is below:

    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
}

On the google app script log Iam getting this error:

   ReferenceError: e is not defined

On the cloud function log the response returned says status: 200, done: true, but the JSON output above is not being returned.

2 Answers 2

2

You might want to try this sort of approach.

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

    function func1(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
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, this solved one problem - that of passing data from the cloud function to the app script. The data is now available in the app script.
However, I still can't receive the data sent from the app script back in the response of the cloud function. The response does not include the JSON output information being returned. Any suggestions for that.
1

The answer above solved the problem of sending data to the app script.

In addition, I did the following to make sure that the response data was correctly returned from app script back to the cloud function. I changed what I was calling from doPost to doGet as below:

 function runScript(auth: any, scriptid: string, testName: string) {
  return new Promise(function (resolve, reject) {
  script.scripts.run(
  {
   auth: auth,
   scriptId: scripted,
   resource: {
     function: "doGet",
     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(response);
   }
 });
});
}

In google app script, I wrote doGet as below:

function doGet(e) {
const returnResult = generateTest(e);
return JSON.stringify(returnResult)
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.