简体   繁体   English

如何使用Google App脚本中的JavaScript从Google电子表格数据库中获取日期值

[英]How do i get the date value from google spreadsheet database using javascript in google app script

I am having a problem on why its showing nothing when i put date in the record on my google spreadsheet database. 我有一个问题,为什么当我在Google电子表格数据库中的记录中放置日期时,它什么也不显示。 Cant figure out why, but when i delete the dates, it shows. 不能弄清楚为什么,但是当我删除日期时,它会显示出来。 I think that javascript cant read the dates from google spreadsheet? 我认为javascript无法从Google电子表格中读取日期? I tried to use new Date() but still not working. 我尝试使用new Date()但仍无法正常工作。 Pls help me with this. 请帮助我。 And also when i put like this 当我这样放

<? 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>

it show the dates. 它显示日期。 So its like when in JS it not reading the date from database. 因此,就像在JS中不从数据库中读取日期一样。 Thats why i need your help guys how it make perfect the codings. 那就是为什么我需要您的帮助人员,它如何使编码完美。

Code.gs Code.gs

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

}

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

Index.html 的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>

So the data that is returned from .getData() is an Object. 因此,从.getData()返回的数据是一个对象。 Google Apps Script's google.script.run doesn't support that. Google Apps脚本的google.script.run不支持该功能。 You can 您可以

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

in your script, then in the SuccessHandler: 在您的脚本中,然后在SuccessHandler中:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 JavaScript/Google App 脚本在 Google 表格中连续映射 arrays - How do I get arrays mapped in a row in google sheet using JavaScript/Google App script 如何将 Ajax 中的数组值插入 Google App 脚本电子表格 - How to Insert Array Value from Ajax into Google App Script Spreadsheet 使用 Google Apps 脚本和 JavaScript 根据电子表格中的多列获取唯一值 - Get Unique Value Based on Multiple Columns from Spreadsheet using Google Apps Script and JavaScript 使用HTTP GET和Google App脚本访问Google电子表格的标题 - Accessing headers of a google spreadsheet using HTTP GET and Google App Script Google-app-script:使用电子表格中的单元格行[]的JavaScript电子邮件正文中的日期格式问题 - Google-app-script: Date formating issue in a javascript email body using a cell row[] in Spreadsheet 通过 Google App Script / Javascript 从电子表格中添加 1 天 - 月份保持重置为当前月份 - Add 1 day to date from spreadsheet via Google App Script / Javascript- Month Keeps Reseting to current month Google App Script/Javascript - 无法使用 Google App Script 循环到 Google 电子表格中的列末尾 - Google App Script/Javascript - Not able to loop through up to the end of Column in Google Spreadsheet using Google App Script 如何使用Google Spreadsheet从用户那里获取文本? - How can I get text from a user using Google Spreadsheet? Google Spreadsheet Script-获取日期作为数字 - Google Spreadsheet Script - get Date as number 使用谷歌应用程序脚本在电子表格的一列中获取价值 - get value in one column in spreadsheet using google apps script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM