How to return the start of the week (start day = sunday) in Google Scripts?

826 views
Skip to first unread message

Calco La

unread,
Nov 1, 2021, 9:46:58 PM11/1/21
to Google Apps Script Community

I have the following code:

function myFunction() { var ss = SpreadsheetApp.getActiveSpreadsheet(); // current spreadsheet var old_sheet = ss.getActiveSheet(); // current (old) sheet var new_sheet = old_sheet.copyTo(ss); // make a copy SpreadsheetApp.flush() new_sheet.setName(Utilities.formatDate(new Date(), 'GMT-7:00', 'w')); // set the name new_sheet.getRange('A3').setValue(new Date(), 'GMT-7:00', 'w'); // set the cell A3 new_sheet.getRange('B14:H18').clear({contentsOnly: true}); // clears content in B14:H18 old_sheet.hideSheet(); // hide the old sheet }

The part I am having trouble with is the following:

new_sheet.getRange('A3').setValue(new Date(), 'GMT-7:00', 'w'); // set the cell A3

My objective is to return the Sunday date into cell A2 OR return the current week number (starting sunday) into cell A3.

For example: 10/31/2021 would be put into A2 or 45 (week number) would be put into A3, from there I can work in either direction using regular formulas in google sheets.

I tried to setvalue(new date() --> using 'w' but ran into an error:

"Exception: The parameters ((class),String,String) don't match the method signature for SpreadsheetApp.Range.setValue."

Which I read here: How do you resolve a "The parameters (number[]) don't match the method signature for SpreadsheetApp.Range.setValues" error

Meaning a range is 2 dimensional, and I tried to use .push() but honestly I am not sure how to do it.

I know if I setvalue(new date()) will give me the current date, but I am not sure how to get the date that starts at the beginning of the week (sunday).

Thank you, if I misspelled or didn't explain correctly do let me know.

Brett Grear

unread,
Nov 2, 2021, 6:01:42 AM11/2/21
to Google Apps Script Community
Shouldn't it be:
new_sheet.getRange('A3').setValue(Utilities.formatDate(new Date(), 'GMT-7:00', 'w')); // set the cell A3?

Your current script is trying to set an array [new Date(), string, string] into one cell which it can't do.

You've already used Utilities.formatDate() in the sheet naming section.

Clark Lind

unread,
Nov 2, 2021, 8:31:57 AM11/2/21
to Google Apps Script Community
Your code is just fine from what I can see. I put the date into its own variable instead of calling it twice. I also added in a check to see if the sheet already exists.
This code worked for me:


function myFunction() {
    var ss = SpreadsheetApp.getActiveSpreadsheet(); // current spreadsheet 
    var old_sheet = ss.getActiveSheet(); // current (old) sheet 
    var date = Utilities.formatDate(new Date(), 'GMT-7:00', 'w');

    if (ss.getSheetByName(date)) {
        SpreadsheetApp.getUi()
            .alert('A sheet by that name already exists.');
        return;
    }

    var new_sheet = old_sheet.copyTo(ss); // make a copy 
    SpreadsheetApp.flush();
    new_sheet.setName(date); // set the name 
    new_sheet.getRange('A3')
        .setValue(date); // set the cell A3 
Reply all
Reply to author
Forward
0 new messages