Reputation: 23
I've written this code which is executed using the Script trigger function in Google Sheets.
function addToList()
{
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s1 = ss.getSheetByName('Accts and Targets'); //source sheet name
var t1 = ss.getSheetByName('Over Time'); //target sheet name
var source = s1.getRange("E21:G21");
var last_row = t1.getLastRow();
var target = t1.getRange("A"+(last_row+1));
t1.insertRowAfter(last_row);
source.copyTo((target), {contentsOnly:true});
var tt = SpreadsheetApp.getActiveSpreadsheet();
var v1 = tt.getSheetByName('Accts and Targets'); //source sheet name
var w1 = tt.getSheetByName('Weekly State'); //target sheet name
var source = v1.getRange("b67:i67");
var last_row = w1.getLastRow();
var target = w1.getRange("A"+(last_row+1));
w1.insertRowAfter(last_row);
source.copyTo((target), {contentsOnly:true});
}
To summarize, the script copies and pastes data from a common source sheet to two different destination sheets.
It is split into 2 portions with a blank line separating the first from the second portion.
Both portions execute daily (as I set it up to do - using the trigger function).
I tried to run the two portions as separate functions and use the trigger functionality, but the second portion didn't work; while the first worked correctly each day.
I'd like some help to insert code that would execute the second portion - BUT only on Mondays.
I see a function called "onWeekDay()", but I can't find the right code to use to have it work.
I suspect I need to add an if statement ahead of this line:
var tt = SpreadsheetApp.getActiveSpreadsheet();
that only executes from this line to the end of the function on Monday's.
Upvotes: 2
Views: 65
Reputation: 64062
function addToList() {
var ss = SpreadsheetApp.getActive();
var s1 = ss.getSheetByName('Accts and Targets');
var t1 = ss.getSheetByName('Over Time');
var source = s1.getRange(21,5,1,3);
t1.insertRowAfter(t1.getLastRow());
var target=t1.getRange(t1.getLastRow()+1,1);
source.copyTo((target),{contentsOnly:true});
if(new Date().getDay()==1) {
var tt = SpreadsheetApp.getActive();
var v1 = tt.getSheetByName('Accts and Targets');
var w1 = tt.getSheetByName('Weekly State');
var source = v1.getRange(67,2,1,8);
w1.insertRowAfter(w1.getLastRow());
var target = w1.getRange(w1.getLastRow()+1,1);
source.copyTo((target), {contentsOnly:true});
}
}
Upvotes: 1