简体   繁体   中英

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

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

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

    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

<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()

  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

parameters and return values

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM