循环播放工作表并将每个表作为数据集返回

时间:2017-04-25 20:55:50

标签: javascript google-apps-script

我写了一个function read来自data的所有spreadsheet,然后尝试parse每个dataset进入array objects的{​​{1}}。

现在,我可以将return作为dataset的{​​{1}} object,但是sheet中的其他数据集也无法做到sectionData[sectionName[object[key][value]]sectionName[object[key][name]]]... }}

这就是我的表格。

enter image description here

我的总体目标是,如果a列中有节名称,则抓取每个数据部分,然后将它们放入sectionData中。每个部分都将由sectionName命名。我希望数组的结构是这样的:

function getSheetSectionDataTest(){
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Params"); // get sheet
  var sheetData = sheet.getDataRange().getValues(); //get all sheet data
  //var sectionNames = normalizeHeaders(sheet.getRange("A1:A").getValues()); //get all data from column A (section names)
  var sectionData = [];
  var section = []; //create empty object for data

  for (var i=0; i<sheetData.length; i++){ //loop through data      
      if (sheetData[i][0] !== ""){ // The loop should stop at each cell in the first column that has a value and run the following code:
        var sectionName = normalizeHeader(sheetData[i]); //return section name found in column A
        //if (sectionName === "types"){
          var headerRow = normalizeHeaders(sheetData[i+1]);
          for (var j = 2; j < sheetData.length; j++) {
              if(sheetData[j][1]!==""){
              var obj = {};
              section.push(obj);   
              for (var rowColumn = 0; rowColumn < headerRow.length; rowColumn++) {

                obj[headerRow[rowColumn]]=sheetData[j][rowColumn+1];
                }              
              }
              else{
                return section; //when the loop runs into an empty cell stop loop and return the data
              }
          }
        //}
      }
    }
};

我编写了一些代码,这些代码将循环并返回第一组的所有数据,但是很难让它为其他集返回数据。

[System.Web.Http.HttpPost]
    public async Task<ContentResult> GetApplePaySession([FromBody] string url)
    {
        // http://stackoverflow.com/a/36912392/1837080
        System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

        // Load the merchant certificate for two-way TLS authentication with the Apple Pay server.
        var certificate = LoadMerchantCertificate();

        // Get the merchant identifier from the certificate to send in the validation payload.
        var merchantIdentifier = GetMerchantIdentifier(certificate);

        // Create the JSON payload to POST to the Apple Pay merchant validation URL.
        var payload = new ApplePayRequest()
        {
            merchantIdentifier = merchantIdentifier,
            domainName = System.Web.HttpContext.Current.Request.Url.Host,
            displayName = "[display name from apple developer portal]"
        };

        JObject merchantSession;

        // Create an HTTP client with the merchant certificate
        // for two-way TLS authentication over HTTPS.
        using (var httpClient = CreateHttpClient(certificate))
        {
            var jsonPayload = JsonConvert.SerializeObject(payload);

            using (var content = new StringContent(jsonPayload, Encoding.UTF8, "application/json"))
            {
                // POST the data to create a valid Apple Pay merchant session.
                using (var response = await httpClient.PostAsync(url, content))
                {
                    response.EnsureSuccessStatusCode();

                    // Read the opaque merchant session JSON from the response body.
                    var merchantSessionJson = await response.Content.ReadAsStringAsync();
                    merchantSession = JObject.Parse(merchantSessionJson);
                }
            }
        }

        // Return the merchant session as JSON.
        return Content(merchantSession.ToString(), "application/json");
    }

    #region Apple Pay helper methods

    private X509Certificate2 LoadMerchantCertificate()
    {
        X509Certificate2 certificate;

        // Load the certificate from the current user's certificate store. This
        // is useful if you do not want to publish the merchant certificate with
        // your application, but it is also required to be able to use an X.509
        // certificate with a private key if the user profile is not available,
        // such as when using IIS hosting in an environment such as Microsoft Azure.
        using (var store = new X509Store(StoreName.My, StoreLocation.LocalMachine))
        {
            store.Open(OpenFlags.ReadOnly);

            // when using thumbprint from mmc, look at:
            // http://stackoverflow.com/a/14852713
            // there is a hidden character that you must delete
            var certificates = store.Certificates.Find(
                X509FindType.FindByThumbprint,
                "[thumbprint]",                    
                validOnly: false);

            if (certificates.Count < 1)
            {
                throw new InvalidOperationException(
                    // ReSharper disable once UseStringInterpolation
                    string.Format(
                        "Could not find Apple Pay merchant certificate with thumbprint '{0}' from store '{1}' in location '{2}'.",
                        "‎[thumpprint]", store.Name, store.Location));
            }

            certificate = certificates[0];
        }

        return certificate;
    }

    private string GetMerchantIdentifier(X509Certificate2 certificate)
    {
        // This OID returns the ASN.1 encoded merchant identifier
        var extension = certificate.Extensions["1.2.840.113635.100.6.32"];

        // Convert the raw ASN.1 data to a string containing the ID
        return extension == null ? string.Empty : Encoding.ASCII.GetString(extension.RawData).Substring(2);            
    }

    private HttpClient CreateHttpClient(X509Certificate2 certificate)
    {
        var handler = new WebRequestHandler();
        handler.ClientCertificates.Add(certificate);

        return new HttpClient(handler, disposeHandler: true);
    }

    #endregion

我的代码将返回1个部分的所有数据,但不会返回其他部分: enter image description here

0 个答案:

没有答案