【问题标题】:Not able to pass parameters from cloud function to google apps script and return a response无法将参数从云函数传递到谷歌应用程序脚本并返回响应
【发布时间】:2020-11-02 20:57:26
【问题描述】:

我正在从我的 firebase 云函数调用一个谷歌应用脚​​本,如下所示。我能够成功调用脚本并创建谷歌表单,但无法 一个。调用 script.run 时将参数/数据从云函数发送到 google app 脚本 湾。在对云函数的响应中,从谷歌应用脚​​本中获取所创建表单的正确数据(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

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

【问题讨论】:

    标签: google-apps-script google-cloud-functions


    【解决方案1】:

    您可能想尝试这种方法。

    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
    }
    

    【讨论】:

    • 谢谢,这解决了一个问题——将数据从云函数传递到应用脚本。数据现在在应用脚本中可用。
    • 但是,我仍然无法在云函数的响应中收到从应用脚本返回的数据。响应不包括返回的 JSON 输出信息。任何建议。
    【解决方案2】:

    上面的答案解决了向app脚本发送数据的问题。

    此外,我还执行了以下操作以确保响应数据正确地从应用脚本返回到云函数。我将我从 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)
    }
    

    【讨论】:

      猜你喜欢
      • 2018-12-10
      • 2023-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-07
      相关资源
      最近更新 更多