【问题标题】:Get dat column in App Script without time zone在没有时区的 App Script 中获取 dat 列
【发布时间】:2021-03-02 05:14:49
【问题描述】:

我正在尝试将日期输入到 App Scripts 中,但最终,它得到了一个带有时间和时区的日期,而我真的不想这样做。 这是我的代码。

function sendMail(){
var first = 0;
var order = 1;
var date = 2;
var item = 3;
var email = 4;
var pesa = 5;
var aliases = GmailApp.getAliases();

var emailTemp = HtmlService.createTemplateFromFile("email");

var ws = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("NAJ");

var data = ws.getRange("A2:F" + ws.getLastRow()).getValues();

data.forEach(function(row){
  emailTemp.fn = row[first]
  emailTemp.oid = row[order]
  emailTemp.odate = row[date]
  emailTemp.itm = row[item]
  emailTemp.amt = row[pesa]
  var htmlMessage = emailTemp.evaluate().getContent();
  GmailApp.sendEmail(
    row[email],
    "Your Order Recently Canceled",
    "Your email doesn't support HTML",
    {'from':aliases[4], name: "My company name", htmlBody: htmlMessage}
    );
  });
}

以下格式显示日期

Thu Feb 25 2021 14:00:00 GMT-0500 (Eastern Standard Time)

我只想这样

25/01/2021
or
01/25/2021

这里是 HTML

<p>Hi <?= fn ?>,</p>
  <p>I hope you are well. We notices you recently tried to place an order on <a href="www.xyz.com">My Company Name</a>, and it got canceled before completion.</p>
  <p>Let me know if you need any assistance with placing an order; I will be delighted to help you.</p>

  <p>Below is your canceled order details:</p>

  <p><strong>Order# <?= oid ?></strong></p>
  <p><strong>Date: <?= odate ?></strong></p>
  <p><strong>Item: <?= itm ?></strong></p>
  <p><strong>Total Amount: $<?= amt ?></strong></p>

我们将不胜感激任何帮助

【问题讨论】:

    标签: javascript google-apps-script google-sheets


    【解决方案1】:

    不清楚您在共享代码中从何处获取日期,但您可以使用 Utilities 类中的 formatDate() 方法随意格式化日期。

    试试这样的:

    let formattedDate = Utilities.formatDate(new Date(), Session.getScriptTimeZone(), "dd/MM/yyyy")
    Logger.log(formattedDate)
    

    看看这是否适合你?

    编辑:这是您可以尝试的完整代码。注意: html-body 是在代码内部构造的。不需要单独的 HTML 文件。

    function sendMail() {
      let [first, order, date, item, email, pesa, aliases] = [0, 1, 2, 3, 4, 5, GmailApp.getAliases()]
      let data = SpreadsheetApp.getActive().getSheetByName("NAJ").getDataRange().offset(1, 0).getValues();
    
      data.forEach(row => {
    
        let formattedDate = isDate_(row[date]) ? Utilities.formatDate(row[date], Session.getScriptTimeZone(), "dd/MM/yyyy") : "";
        let htmlMessage = getHTMLBody_(row[first], row[order], formattedDate, row[item], row[pesa])
    
        if (row[email]) {
          GmailApp.sendEmail(row[email], "Your Order Recently Canceled", "",
            {'from':aliases[4], name: "My company name", htmlBody: htmlMessage }
          );
        }
      });
    }
    
    function getHTMLBody_(fn, oid, odate, itm, amt) {
    
      return '<HTML><p>Hi ' + fn + ',</p>' +
        '<p>I hope you are well. We notices you recently tried to place an order on <a href="www.xyz.com">My Company Name</a> ' +
        'and it got canceled before completion.</p>' +
        '<p>Let me know if you need any assistance with placing an order; I will be delighted to help you.</p>' +
        '<p>Below is your canceled order details:</p>' +
        '<p><strong>Order# ' + oid + '</strong></p>' +
        '<p><strong>Date: ' + odate + '</strong></p>' +
        '<p><strong>Item: ' + itm + '</strong></p>' +
        '<p><strong>Total Amount: ' + amt + '</strong></p>' +
        '</HTML>';
    }
    
    function isDate_(d) {
      return (Object.prototype.toString.call(d) !== "[object Date]") ? false : (isNaN(d.getTime())) ? false : true;
    }

    【讨论】:

    • 我已经更新了完整的代码。我想把日期放在 HTML 中。不知道如何让你的变量去那里。
    • 你的 code.gs 中的“order”、“item”和“pesa”在哪里定义?
    • 它们来自 Google 电子表格。
    • 抱歉,我刚刚又更新了一次。顺便说一句,这些并不重要。我需要格式化日期
    • 我还在数据范围之后添加了一个 offset(1, 0) 以跳过标题行。检查是否有帮助?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-27
    • 2012-01-04
    • 1970-01-01
    相关资源
    最近更新 更多