在Google Script网络应用中格式化客户端的API数据

时间:2017-04-05 21:04:15

标签: javascript api google-apps-script

我通过CRM API提取数据,并在我的Google Script网络应用程序的前端成功呈现该数据。但是为前端操纵或格式化这些数据对我来说是一个挑战。

在下面的代码中,第二行的Potential Name正在向页面呈现正确的数据。但是名为Quote的第一行显示未定义。这个数据是我试图格式化的数据,因此只有最后六个字符或字符串被打印到页面上。

显然,我必须尝试错误地访问API中的数据。有人可以请我提供在Google脚本中操作此数据的正确方法吗?

Code.gs

function doGet() {
    var templ = HtmlService.createTemplateFromFile('Allied-po');
    templ.data = requestRecordFromCRM();
    return templ.evaluate()
        .setTitle('Purchase Order')
        .setSandboxMode(HtmlService.SandboxMode.IFRAME);
}

/*Fetch record data from CRM*/
function requestRecordFromCRM() {
    requestedId = '1234';
    var authToken = 'XXXX';
    var zohoRequestUrl = 'https://crm.zoho.com/crm/private/json/Potentials/getRecordById?&authtoken=' + authToken + '&scope=crmapi&id=' + requestedId;
    var response = UrlFetchApp.fetch(zohoRequestUrl);
    var sanitizedResponse = (response.getContentText());

    /*Sanitize json*/
    var output = JSON.parse(sanitizedResponse);
    Logger.log(output);

    /*Declare the variables you want to print*/
    var parsedOutput = output.response.result.Potentials.row.FL;
    var recordObj = {}

    Logger.log(typeof output)
    Logger.log(output.response.result.Potentials.row.FL.length)

    for (var i = 0; i < output.response.result.Potentials.row.FL.length; i++) {
      if (output.response.result.Potentials.row.FL[i].val == 'Potential Name') {
        recordObj.potentialName = output.response.result.Potentials.row.FL[i].content
      } 
    }

    return (recordObj);
}

的index.html

<html>
    <head>
        <base target="_blank">
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Purchase Order</title>
        <?!= HtmlService.createHtmlOutputFromFile('Stylesheet').getContent(); ?>
    </head>
    <body>
      <div>
         Quote: <span id="job-number"><?= data.potentialName ?></span>
      </div>
      <div>
         Potential Name: <?= data.potentialName ?>
      </div>
        <?!= HtmlService.createHtmlOutputFromFile('Javascript').getContent(); ?>
    </body>
</html>

Javascript.html

    <!-- Load jQuery, jQuery UI, and Bootstrap libraries -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
    <script>
        //Format Job Numbers - return only last six characters in potentialName string
        (function() {

          var parts = document.getElementById('job-number');
          var selectedPart = parts.split(":");
          var thePart = selectedPart[0];

          return (thePart);
        }());
</script>

1 个答案:

答案 0 :(得分:0)

This particular code retrieves the HTML element but not the innerHTML text

 var parts = document.getElementById('job-number');

Instead do this to get the embedded HTML which can used to split the string like so:

var parts = document.getElementById('job-number').innerHTML;

The final code will look like this:

<script>
        //Format Job Numbers - return only last six characters in potentialName string
   (function() {
      var parts = document.getElementById('job-number');
      var selectedPart = parts.innerHTML.split(":");
      console.log(parts)
      console.log(selectedPart)
      var thePart = selectedPart[1];
      parts.innerHTML = thePart
      return (thePart);
    }());

</script>

There is limited information on how the object structure looks like? The assumption here is there is six character before ":" in the data.potentialName value. if you rather want exactly six characters you can do this:

var selectedPart = parts.subString(0,6)

Hope that helps!