同一条消息重复 3 次
Same message repeating 3 times
我有下面的代码,它获取第一列并根据日期将其打印在 google 文档中。不幸的是,这会重复并打印该行 3 次。
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 电子表格中,包含三列 "Announcement"、"Start date" 和 "End date"。我想为 "today" 编制一份公告列表(即今天的日期介于开始日期和结束日期之间)。
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数组。 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 ) );
}
我有下面的代码,它获取第一列并根据日期将其打印在 google 文档中。不幸的是,这会重复并打印该行 3 次。
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 电子表格中,包含三列 "Announcement"、"Start date" 和 "End date"。我想为 "today" 编制一份公告列表(即今天的日期介于开始日期和结束日期之间)。
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数组。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 ) );
}