【问题标题】:How do i get the date value from google spreadsheet database using javascript in google app script如何在谷歌应用脚​​本中使用 javascript 从谷歌电子表格数据库中获取日期值
【发布时间】:2018-02-23 19:50:38
【问题描述】:

当我在我的谷歌电子表格数据库的记录中输入日期时,我遇到了一个问题,为什么它什么也没显示。不知道为什么,但是当我删除日期时,它会显示。我认为 javascript 无法从谷歌电子表格中读取日期?我尝试使用new Date(),但仍然无法正常工作。请帮我解决这个问题。而且当我这样放的时候

<? var data = getData(); ?>
        <table id="tableShift2">
        <caption>Team unknown</caption>
          <th>   Date and Time Plotted   </th>
          <th>   LDAP   </th>
          <th>   Date of VL   </th>
          <th>   HD/WD   </th>
          <th>   Action   </th>
          <? for (var q = 1; q < data.length; q++) {?>
          <tr>
            <td><?=data[q][1];?></td>
          </tr>
          <?}?>
        </table>

它显示日期。所以就像在 JS 中它没有从数据库中读取日期一样。这就是为什么我需要你们帮助他们如何完善编码。

代码.gs

function doGet() {
  return HtmlService
      .createTemplateFromFile('Index')
      .evaluate();

}

function getData() {
  return SpreadsheetApp
      .openById('17lKIhONfEeNogsBhKtGr4zVaLgH0_199-3J3-0tAdcE')
      .getActiveSheet()
      .getDataRange()
      .getValues();
}

Index.html

    <html>
  <head>
    <base target="_top">

  </head>

  <body>
  <? var data = getData(); ?>
  <div id="Options">
    <label><b>Month</b></label>
    <select id="selectMonth">
      <option value="0">-Select Month-</option>
      <option value="1">January</option>       
      <option value="2">February</option>       
      <option value="3">March</option>       
      <option value="4">April</option>       
      <option value="5">May</option>       
      <option value="6">June</option>       
      <option value="7">July</option>       
      <option value="8">August</option>       
      <option value="9">September</option>       
      <option value="10">October</option>       
      <option value="11">November</option>       
      <option value="12">December</option>
    </select> - 
  <input type="radio" name="radioYear" id="radioYear" value="<?= new Date().getYear(); ?>"> <?= new Date().getYear(); ?>
  <input type="radio" name="radioYear" id="radioYear2" value="<?= new Date().getYear()+1; ?>"> <?= new Date().getYear()+1; ?>
  <button id="btnFetch" onclick="google.script.run.withSuccessHandler(fetchRecord).getData()">Fetch</button>
  </div>
  <div  id="tables">
        <table id="tableShift2">
        <caption>Team unknown</caption>
          <th>   Date and Time Plotted   </th>
          <th>   Name   </th>
          <th>   Date of VL   </th>
          <th>   HD/WD   </th>
          <th>   Action   </th>
        </table>
  </div>
  <div id="date"></div>

  <script
src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">
</script>
<script>
$(function() {
  google.script.run.withSuccessHandler(showData)
      .getData();
});

function showData(things) {
  var table = $('#tableShift2');
  for (var i = 0; i < things.length; i++) {
    if (things[i][6] == '') {
    table.append('<tr> <td>' + things[i][1] + 
                 '</td> <td>' + things[i][2] +
                 '</td> <td>' + things[i][3] +
                 '</td> <td>' + things[i][4] +
                 '</td> <td ><button onclick=\'ApproveAndRefresh("' + things[i][0] + '","' + things[i][2] +
                 '")\' id="btnApprove">Approve</button> <button onclick=\'DeclineAndRefresh("' + things[i][0] + '","' + things[i][2] + 
                 '")\' id="btnDecline">Decline</button></td></tr>');
    }
  }
}
function ApproveAndRefresh(data, data1){
  google.script.run
  .withSuccessHandler(refreshData)
  .setApproved(data, data1);
}

function DeclineAndRefresh(data, data1){
  google.script.run
  .withSuccessHandler(refreshData)
  .setDeclined(data, data1);
}

function refreshData(hl){
   document.open();
   document.write(hl);
   document.close();
}

</script>
  </body>
</html>

【问题讨论】:

  • 你能添加一些错误信息吗?检查浏览器控制台和脚本窗口 > 查看 > 执行日志
  • Uncaught Error: The script completed but the returned value is not a supported return type. 是什么意思?

标签: javascript google-apps-script


【解决方案1】:

所以从.getData() 返回的数据是一个对象。 Google Apps 脚本的 google.script.run 不支持该功能。你可以

return JSON.stringify(SpreadsheetApp.openById('17lKIhONfEeNogsBhKtGr4zVaLgH0_199-3J3-0tAdcE')
  .getActiveSheet()
  .getDataRange()
  .getValues()) 

在您的脚本中,然后在 SuccessHandler 中:

function showData(things) {
  things = JSON.parse(things)
  //rest of your code...
}

【讨论】:

  • 怎么样?我需要更改为function getData() { return JSON.stringify(SpreadsheetApp.openById('17lKIhONfEeNogsBhKtGr4zVaLgH0_199-3J3-0tAdcE') .getActiveSheet() .getDataRange() .getValues()) } 吗?我不知道怎么说。
  • 这在您的 Code.gs 中。你把这些值变成一个字符串,然后一旦它回到 SuccessHandler 中,你就把它解析回数组对象。
  • 有效!感谢您的帮助。但是我如何自定义它的时间和日期呢?有格式吗?它显示像这样2018-10-10T07:00:00.000Z。我只需要没有 ¨T 和 Z¨ 的日期和时间。
  • 这是一篇关于customizing javascript Date object的帖子。请注意,您可能必须将包含日期的变量转换为 Date 对象:var time = new Date(things[?][?]); time.getDay() //...etc
  • 谢谢@Chris W。你帮了大忙。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多