如何将对象列表发送到谷歌执行api,并在谷歌应用程序脚本中进行处理



通常,我想将数据从asp.net mvc应用程序导出到Google Sheets,例如人员列表。我已经用我的谷歌帐户(通过OAuth2)建立了连接并验证了应用程序,但现在我正试图将我的对象列表发送到api,然后在脚本中处理它(将所有数据放在新文件中),但我无法理解这一点。

以下是我的应用程序中发送请求的一些示例代码。

public async Task<ActionResult> SendTestData()
    {
        var result = new AuthorizationCodeMvcApp(this, new AppFlowMetadata()).
            AuthorizeAsync(CancellationToken.None).Result;
        if (result.Credential != null)
        {
            string scriptId = "MY_SCRIPT_ID";
            var service = new ScriptService(new BaseClientService.Initializer
            {
                HttpClientInitializer = result.Credential,
                ApplicationName = "Test"
            });

            IList<object> parameters = new List<object>();
            var people= new List<Person>(); // next i'm selecting data from db.Person to this variable
            parameters.Add(people);
            ExecutionRequest req = new ExecutionRequest();
            req.Function = "testFunction";
            req.Parameters = parameters;
            ScriptsResource.RunRequest runReq = service.Scripts.Run(req, scriptId);
            try
            {
                Operation op = runReq.Execute();
                if (op.Error != null)
                {
                    // The API executed, but the script returned an error.
                    // Extract the first (and only) set of error details
                    // as a IDictionary. The values of this dictionary are
                    // the script's 'errorMessage' and 'errorType', and an
                    // array of stack trace elements. Casting the array as
                    // a JSON JArray allows the trace elements to be accessed
                    // directly.
                    IDictionary<string, object> error = op.Error.Details[0];
                    if (error["scriptStackTraceElements"] != null)
                    {
                        // There may not be a stacktrace if the script didn't
                        // start executing.
                        Newtonsoft.Json.Linq.JArray st =
                            (Newtonsoft.Json.Linq.JArray)error["scriptStackTraceElements"];
                    }
                }
                else
                {
                    // The result provided by the API needs to be cast into
                    // the correct type, based upon what types the Apps
                    // Script function returns. Here, the function returns
                    // an Apps Script Object with String keys and values.
                    // It is most convenient to cast the return value as a JSON
                    // JObject (folderSet).
                    Newtonsoft.Json.Linq.JObject folderSet =
                            (Newtonsoft.Json.Linq.JObject)op.Response["result"];
                }
            }
            catch (Google.GoogleApiException e)
            {
                // The API encountered a problem before the script
                // started executing.
                AddAlert(Severity.error, e.Message);
            }

            return RedirectToAction("Index", "Controller");
        }
        else
        {
            return new RedirectResult(result.RedirectUri);
        }
    }

接下来是如何在脚本中处理这些数据——它们在那里被序列化为JSON吗?

执行API调用本质上是REST调用,因此有效负载应按此进行序列化。字符串化的JSON通常很好。然后,GAS函数应该解析该有效载荷以使用编码列表

var data = JSON.parse(payload);

相关内容

  • 没有找到相关文章

最新更新