Home > Mobile >  How To Display Weekday in Google Sheet Script?
How To Display Weekday in Google Sheet Script?

Time:10-16

Please Forgive me if this has been covered couldn't find it. But Does anyone have an idea on how to get a weekday (monday, Tues, Ect...) in this Script i have running.

function newColumn() {
   var ss = SpreadsheetApp.getActiveSpreadsheet();
   var sheet = ss.getSheetByName('Song of the Day');
   sheet.insertColumnAfter(1);
   sheet.getRange("B1").setValue(new Date()).setNumberFormat('mmm/d/yyyy');
}

Currently ^ It makes a new column B and adds the date in cell B1 Every day I would just like that date to remain the same but also display the day of the week beside it also Thanks in advance!

CodePudding user response:

If I understand right you want Column B to contain the date, and you want that to format like 'mmm/d/yyyy' and then you want column C to also have the date but to be formatted to show the Name of the Day.

I would modify your script to place the date unformatted into both columns B and C, and then use the formatting controls in your spreadsheet to display them how you want to.

Your code would change to:

function newColumn() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName('Song of the Day');
  sheet.insertColumnAfter(1);
  sheet.getRange("B1").setValue(new Date());
  sheet.insertColumnAfter(2);
  sheet.getRange("C1").setValue(new Date());
}

On Your spreadsheet choose the Column C, then from Format>Custom Date and Time choose one with the Name of the Day listed, and remove the parts you don't want until it looks like this: custom date image "Day Name Only"

If you wanted to format it with code then as shown here Javascript in Google Sheets script: help using setNumberFormat

you should grab the cell and format the cell:

var cell = sheet.getRange("B1");
cell.setNumberFormat('mmm/d/yyyy');
var cell = sheet.getRange("C1");
cell.setNumberFormat('dddd ');

Date number Formattings found here: https://developers.google.com/sheets/api/guides/formats

CodePudding user response:

Display Day of Week:

function DisplayDayOfWeek() {
  Logger.log(["Sun","Mon","Tue","Wed","Thu","Fri","Sat"][new Date().getDay()])
}
  • Related