I have code below that takes the first column and prints it in a google doc based on the date. Unfortunately, this repeats and prints the row three times.
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);
}
}
The source data is in a Google spreadsheet, with three columns "Announcement", "Start date" and "End date". I want to compile a list of announcements for "today" (i.e. today's date is between the start & end dates, inclusively).
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 |
Here is an example of my output:
My code is skipping the announcements that should not bee seen, that's good. But it's printing the expected announcements multiple times. How can I fix that?