简体   繁体   English

从Google电子表格检索单元格日期到HTML服务

[英]Retrieve cell date from a google spreadsheet to HTML service

I have a google apps script which serves HTML. 我有一个提供HTML的Google Apps脚本。 It retrieves data from a google spreadsheet and renders it as a html table. 它从Google电子表格中检索数据,并将其呈现为html表。

It works well except when there is a date on the cell, I get the following error on the JS console: 效果很好,除非在单元格上有日期时,我在JS控制台上收到以下错误:

Uncaught ScriptError: Se ha ejecutado la secuencia de comandos, pero el tipo de valor que se muestra es incompatible.

It is in Spanish, but means: the cmd sequence was executed, but the type of data showed is not compatible. 它是西班牙语,但表示:已执行cmd序列,但显示的数据类型不兼容。

HTML 的HTML

<script type="text/javascript">
$(document).ready(function(){
$("#tabs").tabs();
google.script.run.withSuccessHandler(fillTabs).getAllTabsData();

}); 

</script>

I think the problem is in this function, because it fails in every script that I implement it: 我认为问题出在此函数中,因为它在我实现它的每个脚本中均失败:

function fillTabs(tabs) {
/*Tabs debe ser un array de objetos. cada objeto debe tener, name refiriendose a la pestaña y un array bidimensional con los datos de esa pestaña [rows][columns]*/
  var tabsDiv;//=$('#tabs');
  var table;
  var data;
  console.log("=Array tabs: "+tabs);
  for (var i = 0; i < tabs.length; i++) {

    data=tabs[i].data;
    var tabName=tabs[i].name;
    console.log("==Procesando: "+tabName);
    tabsDiv=$('#'+tabName);
    table='<table class="mytable"><tr>'    

    //iterando columnas primera fila (HEADERS)
    for(var x=0; x<data[0].length;x++){
        table+='<th>'+data[0][x]+'</th>'
    }
    table+=' <th width="20%">Action</th></tr>'; //cabeceras fin

    for( var j=1; j<data.length; j++){//iterando rows. Empezamos en la 1 porque 0=headers
      table+='<tr id="'+j+'">'
      for(var k=0; k<data[j].length;k++){//iterando columns
        table+='<td>'+data[j][k]+'</td>'
      }
      table+='<td></td></tr>'
    }
    table+='</table/>'

    tabsDiv.html(table);
  }
  $("tr:odd").addClass("odd");

};

GS GS

function getAllTabsData(){
  var tabNames = getColumnData('Types'); 
  var sSheet =SpreadsheetApp.openById(key);
  var tabsObject=[];

  for(var i=0; i<tabNames.length;i++){
    tabsObject.push(
      { name:tabNames[i],
       data:sSheet.getSheetByName(tabNames[i]).getDataRange().getValues()});

  }
Logger.log(tabsObject);
  return tabsObject;
}

I don't get any error on the GS part, just in the HTML. 我在GS部分没有任何错误,只是在HTML中。 All the JavaScript code after that is not being executed. 之后的所有JavaScript代码均未执行。

I checked other similar script and it works with dates. 我检查了其他类似的脚本,它适用于日期。 You can see it here https://script.google.com/macros/s/AKfycbwr9wDJibSknCy4thjj6Hedz8H9NOq2Ren6NLqBUhf6nzVYquFL/exec 您可以在这里查看它https://script.google.com/macros/s/AKfycbwr9wDJibSknCy4thjj6Hedz8H9NOq2Ren6NLqBUhf6nzVYquFL/exec

The only difference is that does not returns an array of objects (like mine does), it only returns a single array. 唯一的区别是不返回对象数组(就像我的一样),它仅返回单个数组。

I don't understand why this does not work "by deafault", but as I suspected, JSON did the JOB 我不明白为什么这不能通过“默认”起作用,但是正如我所怀疑的那样,JSON确实使JOB发挥了作用

Adding JSON.stringify on the server side and 在服务器端添加JSON.stringify并

JSON && JSON.parse(json) || $.parseJSON(json);

on the client side worked. 在客户端工作。

I copied that line of code. 我复制了那行代码。 I understand the part after the ||, but I don't understand why to use JSON && JSON.parse. 我了解||之后的部分,但不了解为什么要使用JSON && JSON.parse。 I suppose is to avoid trying JSON.parse if JSON is not available. 我想是避免在JSON不可用的情况下尝试使用JSON.parse。

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

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