简体   繁体   English

当我尝试从 Google 表格中获取数据时,如果此数据包含日期或时间,则不会将其提取到 Google 应用脚本中

[英]When I try to fetch data from Google Sheets, if this data contains a date or time, it is not fetched into the Google app script

I searched for this info in many websites, but unfortunately I did not find it.我在很多网站上搜索过这个信息,但不幸的是我没有找到。 I used an app script from Google to fetch data from Google Sheet The code I used to get data from google sheet;我使用 Google 的应用程序脚本从 Google Sheet 获取数据我用来从 Google Sheet 获取数据的代码; Can't pass any data contains date or time, such as 2021/01/01 or 12:00 AM or 12:00:00 but when removing date or time fetching data does work fine无法传递任何包含日期或时间的数据,例如2021/01/0112:00 AM12:00:00但删除日期或时间时获取数据确实可以正常工作

Technologies used: google app script, google sheet使用的技术:谷歌应用程序脚本,谷歌表

The question is: How to fetch data, regardless of its type?问题是:如何获取数据,而不管其类型?

Code.gs It works as it should Code.gs正常工作

var ss1 = "1bqKXcD2uXUIegqsb4pwieuhwe29-3872_Kj50kfvvXU";
function getLicenseExpirationData(){

  //Get spread sheet with id = to var ss1
  var ss = SpreadsheetApp.openById(ss1);
  //Get spread sheet named Reports
  var ws = ss.getSheetByName("Reports");
  //Get data from a spread sheet
  var LicenseExpirationData = ws.getRange(2, 1,ws.getLastRow()-1,9).getValues();
  //After geting all data return it so we can use it
  return LicenseExpirationData;
};

TableScript.html I guess the problem is here TableScript.html我猜问题出在这里

    document.addEventListener("DOMContentLoaded",function(){
    //Get all result from google sheet and if you did get it display it or do display error
    google.script.run.withSuccessHandler(generateExpirationTable).getLicenseExpirationData();

    });
    //function name generateTable
    function generateExpirationTable(dataArray){
    //Display result inside in id with name ExpirationData
    var tbody = document.getElementById("ExpirationData");
    //Do this for each result
    dataArray.forEach(function(r){
        //Create Element <tr>
        var row = document.createElement("tr");
        //Create Element <td> for column1
        var column1 = document.createElement("td");
        //Display result1 inside <td>
        column1.textContent = r[0];
        //Create Element <td> for column2
        var column2 = document.createElement("td");
        //Display result2 inside <td>
        column2.textContent = r[1];
        //Create Element <td> for column3
        var column3 = document.createElement("td");
        //Display result3 inside <td>
        column3.textContent = r[2];
        //Create Element <td> for column3
        var column4 = document.createElement("td");
        //Display result4 inside <td>
        column4.textContent = r[3];
        //Create Element <td> for column3
        var column5 = document.createElement("td");
        //Display result5 inside <td>
        column5.textContent = r[4];
        //Create Element <td> for column3
        var column6 = document.createElement("td");
        //Display result6 inside <td>
        column6.textContent = r[5];
        //Create Element <td> for column3
        var column7 = document.createElement("td");
        //Display result7 inside <td>
        column7.textContent = r[6];
        //Create Element <td> for column3
        var column8 = document.createElement("td");
        //Display result8 inside <td>
        column8.textContent = r[7];
        //Create Element <td> for column3
        var column9 = document.createElement("td");
        //Display result9 inside <td>
        column9.textContent = r[8];
        //Create Element <td> for column3
        var column10 = document.createElement("td");
        //Display result10 inside <td>
        column10.textContent = r[9];
        //Move to next column
        row.appendChild(column1);
        //Move to next column
        row.appendChild(column2);
        //Move to next column
        row.appendChild(column3);
        //Move to next column
        row.appendChild(column4);
        //Move to next column
        row.appendChild(column5);
        //Move to next column
        row.appendChild(column6);
        //Move to next column
        row.appendChild(column7);
        //Move to next column
        row.appendChild(column8);
        //Move to next column
        row.appendChild(column9);
        //Move to next row
        row.appendChild(column10);
        //Move to next row
        tbody.appendChild(row);
    
    });
    };
    function showError() {
        alert('Error!');
    }

View items as Html It works as it should以 Html 格式查看项目它应该可以正常工作

<table class="mt-0 pt-0 table">
      <thead>
        <tr>
        <th>Date</th>
        <th>Name</th>
        <th>Num</th>
        <th>Data2</th>
        <th>Data3</th>
        <th>Data4</th>
        <th>Data5</th>
        <th>Data6</th>
        <th>Data7</th>
        <tr>
      </thead>
      <!--Display table result on id body-table-->
      <tbody id="ExpirationData">
      </tbody>
   </table>

Two things:两件事情:

  1. When retrieving date object from Google Sheets it is best to use getDisplayValues() instead of getValues()从 Google Sheets 检索日期对象时,最好使用getDisplayValues()而不是getValues()

  2. When passing arrays from code.gs to clientside - pass it as a string, that is: JSON.stringify() the array on Apps Script side before returning it a JSON.parse() it again on Javascript side将数组从 code.gs 传递到客户端时 - 将其作为字符串传递,即: JSON.stringify() Apps Script 端的数组,然后在 Javascript 端再次返回JSON.parse()

parameters and return values 参数和返回值

暂无
暂无

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

相关问题 使用 Google 应用脚本从多个工作表中获取数据 - Fetch data from multiple sheets using Google app script 如何获取 JSON 数据并将其解析为 Google 表格脚本? - How do I fetch and parse JSON data to Google Sheets Script? 将数据提取到 Google 表格 - Fetch data to Google Sheets 尝试使用Google脚本从Google表格中获取数据并在具有水平标签的网页上显示 - Trying to fetch data from Google Sheets using Google script and displaying on web page having horizontal tab 使用 Google 表格中的应用脚本获取网站数据 - Fetch website data using app scripts in Google sheets 触发运行 Google Sheets 脚本以批量从 Yahoo Finance 获取 URL 数据 - Trigger-run a Google Sheets script to fetch URL data from Yahoo Finance in batches 从 Google 表格中提取数据以在 Google 脚本上制作甘特图并将 HTML 作为网络应用程序提供 - Ingest data from Google sheets to make a gantt chart on Google script and serve the HTML as web app 使用 Google 表格中的数据在 Google Apps 脚本中自动完成 - Autocomplete in Google Apps Script with data from Google Sheets 复制到 Google 表格脚本,在下一列中添加日期和源数据 - copyto Google Sheets Script adding date and source data in the next column 在使用谷歌应用脚本将数据从谷歌表格注入谷歌幻灯片期间将日期转换为 dd/mm/yyyy - Convert date to dd/mm/yyyy during injection of data from google sheets to google slides using google apps script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM