我的代码如下,第一列是根据日期在google doc中打印出来的。不幸的是,这会重复并打印三次。
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 |
我的代码正在跳过不应该看到的公告,这很好。但它多次打印预期的公告。我该如何解决这个问题?
答案 0 :(得分:0)
这是代码的最小版本,用于演示问题,并添加了评论说明发生了什么:
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的Spreadsheet方法时,电子表格行(和列)从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 ) );
}