无法将参数从云函数传递到谷歌应用程序脚本并返回响应



我正在从firebase云函数调用一个谷歌应用程序脚本,如下所示。我能够成功调用脚本并创建谷歌表单,但无法a.调用script.run时,从cloud函数向google应用程序脚本发送参数/数据b.在对cloud函数的响应中,从google应用程序脚本中为创建的表单获取正确的数据(url(。

我是应用程序脚本的新手。请帮我理解我做错了什么。

我的云功能代码:

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

我的谷歌应用程序脚本如下:

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
}

在谷歌应用程序脚本日志上,我得到了这个错误:

ReferenceError: e is not defined

在云函数日志上,返回的响应显示status:200,done:true,但没有返回上面的JSON输出。

您可能想要尝试这种方法。

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
}

上面的答案解决了向应用程序脚本发送数据的问题。

此外,我还做了以下操作,以确保响应数据从应用程序脚本正确返回到云功能。我将我所调用的内容从doPost更改为doGet,如下所示:

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

在谷歌应用程序脚本中,我写了doGet如下:

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

相关内容

  • 没有找到相关文章

最新更新