gpt4 book ai didi

javascript - 在没有时区的App Script中获取数据列

转载 作者:行者123 更新时间:2023-12-04 15:04:25 25 4
gpt4 key购买 nike

我正在尝试将日期输入到应用程序脚本中,但最终,它得到了一个我真的不想要的带有时间和时区的日期。这是我的代码。

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

以下面的格式显示日期

2021 年 2 月 25 日星期四 14:00:00 GMT-0500(东部标准时间)

我只想要它

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>

任何帮助将不胜感激

最佳答案

不清楚您在共享代码中从何处获取日期,但您可以使用 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;
}

关于javascript - 在没有时区的App Script中获取数据列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66433689/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com