简体   繁体   English

使用Google Apps脚本HTML服务设置表格日期列格式

[英]Table date column formatting with google apps script html service

I am having problems figuring out basic table formatting. 我在弄清楚基本表格式方面遇到问题。 I am hoping someone can see what I have and suggest a way of changing the outcome. 我希望有人能看到我所拥有的,并提出改变结果的方法。 My Google spreadsheet has 2 columns 'Number' and 'Date'. 我的Google电子表格有2列“数字”和“日期”。

My CodeGS looks like 我的CodeGS看起来像

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

function getData() {
  return SpreadsheetApp
      .openById('1111111111111111111111')
      .getActiveSheet()
      .getDataRange()
      .getValues();

} 

My Index.html 我的Index.html

<? var data = getData(); ?>
<!DOCTYPE html>
<html>
<Style>
 body {
    font-family: arial;
  }

  table {
    border: 1px solid #ccc;
    width: 100%;
    margin:0;
    padding:0;
    border-collapse: collapse;
    border-spacing: 0;
  }

  table tr {
    border: 1px solid #ddd;
    padding: 5px;
  }

  table th, table td {
    padding: 10px;
    text-align: left;
  }

  table th {
    text-transform: uppercase;
    font-size: 14px;
    letter-spacing: 1px;
  }
  </Style>
  <head>
  </head>
  <body>
<table>
   <thead>
    <tr>
      <th>Number</th>
      <th>Date</th>
    </tr>
  </thead>     
     <? for (var i = 1; i < data.length; i++) { ?>
        <tr>
          <? for (var j = 0; j < data[i].length; j++) { ?>
            <td><?= data[i][j] ?></td>
          <? } ?>
        </tr>
      <? } ?>

</table>

  </body>
</html>

When executed the Date column shows a value like 'Mon Dec 05 2016 00:00:00 GMT-0600 (CST)' 执行时,“日期”列将显示一个类似于“ 2016年12月5日星期一00:00:00 GMT-0600(CST)”的值

In the spreadsheet and what I would like to see is something like '12/5/2016' 在电子表格中,我希望看到类似“ 12/5/2016”的内容

I am hoping someone can show me how to make this work 我希望有人可以告诉我如何进行这项工作

Regards, 问候,

Chris 克里斯

This is by no means an elegant solution but to completely control the date format for display I use the following: 这绝不是一个优雅的解决方案,而是完全控制显示的日期格式,我使用以下方法:

var dateStr = 'Mon Dec 05 2016 00:00:00 GMT-0600 (CST)';

function pad(n){
  var pad = "";  
  if(n < 10){pad = "0" + n}else{pad = n};  
  return pad;
}

function shortDate(dateString){

  var d = new Date(dateString);

  var curr_date = d.getDate();
  var curr_month = d.getMonth() + 1;//zero indexed
  var curr_year = d.getFullYear();

  var shortDate = pad(curr_date) + "/" + pad(curr_month) + "/" + curr_year;

  return shortDate;

 }

 console.log(shortDate(dateStr));

This code will output a UK format date, you'll want to switch pad(curr_date) and pad(curr_month). 此代码将输出英国格式的日期,您需要切换pad(curr_date)和pad(curr_month)。

This does mean that you would have to write a table generating function to replace the way in which your table is generated currently as you need to handle the date values independently. 这确实意味着您必须编写一个表生成函数来替换当前生成表的方式,因为您需要独立处理日期值。

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

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