0

一般来说,我想将数据从 asp.net mvc 应用程序导出到 Google 表格,例如人员列表。我已经使用我的 Google 帐户(通过 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 吗?

4

1 回答 1

1

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

var data = JSON.parse(payload);
于 2016-05-09T16:51:42.560 回答