Cell date via script - google-apps-script

function archivr() {
var ss = SpreadsheetApp.getActive();
var sh = ss.getSheetByName('Página1');
sh.getRange('A2').setValue(Utilities.formatDate(new Date(), "GMT-3", "YYYY-MM-DD"));
}
I'm trying to use this script format to put today's date in my spreadsheet, but when I try to put it, the result is:
2020-02-52
Day 52? There is no logic to this, I don't know what's going on.
Additional, how can I also add tomorrow's date in A3?

As written in the official documentation, D is Day in year. Use dd instead.

Related

How To Display Weekday in Google Sheet Script?

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!
Display Day of Week:
function DisplayDayOfWeek() {
Logger.log(["Sun","Mon","Tue","Wed","Thu","Fri","Sat"][new Date().getDay()])
}
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

Google Scripts Format Date Creates Impossible Date

In a very stripped down sense, this is my issue. I am creating a script that will look for a folder with a given name inside a particular folder. If the script finds a folder with that name, it will move the current document to that folder. If not, it should create a folder with that name, and then move the document.
The current naming convention on folders is "Billed 6/18", as an example.
Right now my script is throwing the date out in a weird way, and that won't help me with my later search by file name in a string format.
function MoveTicket() {
var ss = SpreadsheetApp.getActive()
var billfold = DriveApp.getFolderById(id)
var currentdate = Utilities.formatDate(new Date(), "GMT-4", "M/D")
var billfoldname = ('Billed ' + currentdate)
Logger.log(billfoldname)
}
But my log shows [20-06-18 15:41:59:480 EDT] Billed 6/170
I've attempted changing the timezone from GMT-4 to UTC, or changing the date format to M/DD or MM/DD, and I still see the same issue. Just logging new Date() does show the correct date.
Where is it getting 170 for the date? How can I correct it?
Solved. Was using D which gave day of the year instead of d which gave day of the month.
var currentdate = Utilities.formatDate(new Date(), "GMT-4", "M/d")
Simple Date Format

How to show a default current date in a cell in Google Sheet where data validation is used for selecting date?

I have a cell in Google sheet where a user can select a date. I did it using data validation based on the criteria of Is Valid Date. Now I would like to show/use a default today's date in the (yyyy-mm-dd) format in that cell when the Google spreadsheet is opened. I used the following code inside onOpen():
var spreadsheet = SpreadsheetApp.getActive();
var dashboard = spreadsheet.getSheetByName("Dashboard");
dashboard.getRange("C4").setValue(now);
//dashboard.getRange("D4").setValue(now);
Please note that the cell is C4. But it does not work. So how to solve it easily?
This seemed to work for me:
I used a different but you can modify as needed
function datetesting() {
const ss=SpreadsheetApp.getActive();
const sh=ss.getActiveSheet();
let rule=SpreadsheetApp.newDataValidation().requireDate().build();
sh.getRange(1,9,10,1).setDataValidation(null);
sh.getRange(1,9,10,1).setDataValidation(rule);
sh.getRange(1,9).setValue(new Date());
}

Is there a way to insert yesterday's date statically in a cell using a macro in google sheets?

I have a macro to run at the beginning of every day which inserts today's date, grabs the previous day's data inputs, pastes them below today's date, and then formats it all. The issue is that I need it to display yesterday's date, not today's, and I need it to remain that date over time.
I've searched all over for solutions to this issue, but I can't find anything that answers my issue. Maybe I'm just bad at searching. This is what the code in question currently is:
function MorningRoutine() {
var spreadsheet = SpreadsheetApp.getActive();
var date = new Date();
spreadsheet.getActiveRangeList().setValue(date);
I need the macro to input yesterday's date, but this currently displays today's.
This will give yesterday date:
function MorningRoutine() {
var spreadsheet = SpreadsheetApp.getActive();
var date = new Date();
date.setDate(d.getDate()-1);
spreadsheet.getActiveRangeList().setValue(date);
}

Google Script; How to SUM two date and return the result in google spreadsheet

Data:
E3 = 5/12/2013 18:26:00
C3 = 00:15:00
Result should be = 5/12/2013 18:41:00
Problem:
I want an script that sum E3 + C3 and return the value. I needs this to integrate the information with other script.
I try to SUM using script, but in my research is needs some kwnowloge regarding milliseconds the date to bit or something like this to sum date.
The resuld should be in that format dd/mm/yyyy HH:MM:SS (24hs) because is used to call other script.
Test Case:
Workaround: Here
I will appreciate if anyone can help. I think this script will helpfully some much because is a general function.
One option is to establish a formula, in a new cell, e.g.:
The following code sets the formula:
/* CODE FOR DEMONSTRATION PURPOSES */
function dateAdd() {
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var range = ss.getRange('C1').setFormula('A1+B1');
var formattedDate = Utilities.formatDate(range.getValue(), 'GMT', 'dd/MM/yyyy HH:mm:ss');
//Logger.log(formattedDate); <-- 05/12/2013 18:41:00
}
It is important to correctly define the timezone of the spreadsheet and script.