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.