我下面的代码获取第一列并根据日期将其打印在谷歌文档中。不幸的是,这会重复并打印该行三次。
function saveAsDOC() {
var fileName = "Announcements Test File";
var doc = DocumentApp.create(fileName);
// Get the range in the spreadsheet
var ws = SpreadsheetApp.getActiveSpreadsheet().getDataRange();
try {
var data = ws.getValues();
// Announcements loop
if (data.length > 1) { // Make sure data is longer than one character
for (var row = 1; row < data.length; row++) {
if(new Date(data[row][2]) <= new Date()) { // Check the date
for (var col = 1; col < data[row].length; col++) {
if(data[row][col]!=""){ // Make sure the data is not empty
doc.appendParagraph(data[row][1]); // Append announcement
doc.appendParagraph("") // Line break between announcements
}
}
}
}
}
}
catch(err) {
Logger.log(err);
Browser.msgBox(err);
}
}
源数据位于 Google 电子表格中,包含“公告”、“开始日期”和“结束日期”三列。我想编制“今天”的公告列表(即今天的日期在开始日期和结束日期之间,包括开始日期和结束日期)。
A B C
1 | Announcement | Start date | End date |
2 | You SHOULD see this. | 1 Jan 2015 | 31 Dec 2015 |
3 | You SHOULD NOT see this. | 1 Jan 2015 | 1 Jan 2015 |
4 | You SHOULD ALSO see this. | 1 Jan 2015 | 31 Dec 2015 |
5 | You SHOULD NOT see this. | 31 Dec 2015 | 31 Dec 2015 |
我的代码跳过了不应该看到的公告,这很好。但它多次打印了预期的公告。我该如何解决这个问题?
最佳答案
这是演示问题的代码的最小版本,并添加了注释来解释发生的情况:
function announcements() {
// Get the range in the spreadsheet
var ws = SpreadsheetApp.getActiveSpreadsheet().getDataRange();
var data = ws.getValues();
var results = [];
if (data.length > 1) { // Make sure data is longer than one ROW (not character)
for (var row = 1; row < data.length; row++) { // Skips first row, then loop over the rest is that what you want?
if(new Date(data[row][2]) <= new Date()) { // Check the date, [2] is the third column.
for (var col = 1; col < data[row].length; col++) { // Loop over all columns except the first one
if(data[row][col]!=""){ // Make sure the data is not empty
results.push(data[row][1]); // Append [row][1]; **** Same value
}
}
}
}
}
Logger.log( JSON.stringify( results ) );
}
您可能会遇到一些问题:
-
if (data.length > 1)
检查您是否有超过一行的数据,这与您的评论不匹配。确保这是您想做的事情。 -
for (var row = 1; etc.
循环遍历所有行,但跳过第一行。也许这就是您想要的,但人们遇到的一个常见问题是,当您使用 Google 的电子表格方法时,电子表格行(和列)从 1 开始编号,但在 JavaScript 数组中从 0 开始编号。data
的内容是一个 JavaScript 数组,所以它从 0 开始。(在 chat 中讨论过,这是可以理解的,所以没有问题。) -
if(new Date(data[row][2]) <= new Date())
仅检查“结束日期”是否是截至今天的日期。您还需要检查“开始日期”。 (在聊天中讨论。) -
for (var col = 1; col < data[row].length; col++)
循环遍历所有列(每行)。然后if(data[row][col]!="")
检查该单元格是否非空白,并且results.push(data[row][1])
附“公告”。 这就是您看到重复公告的原因。根据您的问题陈述,不需要遍历该行,您只需检查每一行是否有公告。
更新代码
function announcements() {
// Get the range in the spreadsheet
var ws = SpreadsheetApp.getActiveSpreadsheet().getDataRange();
var data = ws.getValues();
var announcementCol = 1, // "constants" for column numbers
dateCol = 2;
var results = []; // Array to hold announcements for today
if (data.length > 1) {
// Loop over rows that contain announcements
for (var row = 1; row < data.length; row++) {
// Check that today is a date that the announcement should be included.
if(new Date(data[row][1]) <= new Date() && new Date(data[row][2]) >= new Date() ) {
// Is there an announcement in this row?
if(data[row][announcementCol]!=""){
// Add this announcement to results
results.push(data[row][announcementCol]);
}
}
}
}
// Show results in log
Logger.log( JSON.stringify( results ) );
}
关于javascript - 同一条信息重复3次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32257229/