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.
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.
{  "config": {    "url": "https://script.googleapis.com/v1/scripts/MY_SCRIPT_ID:run",    "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": {      "@type": "type.googleapis.com/google.apps.script.v1.ExecutionResponse"    }  },  "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": {    "responseURL": "https://script.googleapis.com/v1/scripts/MY_SCRIPT_ID:run"  }}
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-community+unsub...@googlegroups.com.