gpt4 book ai didi

google-apps-script - Google Apps 脚本格式

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

首先为糟糕的礼仪或愚蠢的问题道歉,这是我第一次尝试在谷歌电子表格上编写脚本,我必须说我比在 excel 中使用 VBA 更困难。无论如何我的问题;

我有一个四列五十三行的电子表格。列保持;周数(例如 33)、周开始日期(例如 2012 年 8 月 13 日)、工作时间(例如 31:15:00)和应付工资(例如 150.00 美元)。
第一行有标题,第 2 到 53 行是一年中的第几周。
露营太容易了。现在我已经设法编写了一个脚本,运行时会通过电子邮件向我发送一周的摘要电子邮件。我的代码如下;

  function sendEmail() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Summary");
var startRow = 2; // First row of data to process
var numRows = sheet.getLastRow()-1; // Number of rows to process
// Fetch the range of cells A2:B3
var dataRange = sheet.getRange(startRow, 1, numRows, sheet.getLastColumn());
// Fetch values for each row in the Range.
var data = dataRange.getValues()
//Logger.log(data)

for (i in data) {
var row = data[i];
var date = new Date();
var sheetDate = new Date(row[1]);
Sdate=Utilities.formatDate(date,'GMT-1000','w')
SsheetDate=Utilities.formatDate(sheetDate,'GMT-1000', 'w')
//Logger.log(Sdate+' =? '+SsheetDate)
if (Sdate == SsheetDate){
var emailAddress = row[0]; // First column
var weeknum = row[0];
var week = row[1];
var hours = row[2];
var pay = row[3];
var subject = "Timesheet Report for Week " + weeknum;
MailApp.sendEmail('me@myemail.com',
subject,
"Chris, \n\nBased on the timesheet data you have submitted last week So and So has recorded the following hours and should be due the calculated pay.\n\n" +
"Week Beginning; " + week +
"\n\nTotal Hours Worked For The Week; " + hours +
"\n\nPay Due For The Week; " + pay);
//Logger.log('SENT :'+emailAddress+' '+subject+' '+message)
}
}
}

我现在的问题是我希望一周开始显示“8 月 13 日星期一”,总工作时间显示时间,支付显示为货币。但是,该脚本不保留电子表格中的格式。

最佳答案

Apps 脚本中无法获取电子表格可视格式的单元格,只能获取它们背后的真实值。有getNumberFormat方法,您可以在其中获取用户可能已应用于单元格的任何特定格式。但是这种方法不能按预期工作,因为它无法在很多情况下检索正确的格式,例如货币和百分比值,也不会检索默认格式,只有当用户更改它们时。

在您的情况下,仅在代码本身中格式化您的值可能更容易,因为您手动连接它们。要格式化日期,您可以使用 Utilities.formatDate , 对于数字,您可能可以使用普通的 javascript 函数自行解决,如 num.toFixed()以及货币符号的一些字符串连接。

顺便说一句,我开发了一个脚本来合并邮件并具有这样的格式化功能,你可能会发现它比这个片段更适合你。它被称为 FormEmailer,可在 Script Gallery 和 its site 上找到。 .

关于google-apps-script - Google Apps 脚本格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12023936/

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