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
Related
I'm trying to fill some google sheets cells with the current date and time, date is ok, but the time is off by 6 hours.
This is the code I used:
function start() {
var ss = SpreadsheetApp.getActive();
var timestamp = new Date();
Logger.log('start, timestamp: ' + timestamp);
var date = Utilities.formatDate(timestamp, 'Europe/Brussels', "dd/MM/yyyy");
Logger.log('date: ' + date);
var time = Utilities.formatDate(timestamp, 'Europe/Brussels', "HH:mm:ss");
Logger.log('time: ' + time);
var sheet = SpreadsheetApp.getActive().getSheetByName(SheetName);
sheet.appendRow([date,time]);
}
The logs of the date and time do give me the expected date and time, but once the appendRow is done, in Google Sheets, I'm getting the offset time...
I have tried to offset it myself, to correct to the right value, but that didn't change anything.
That is odd, I tested your code and it works for me.
It's properly showing the timezone of 'Europe/Brussels'. Also, tested using var time = Utilities.formatDate(timestamp, 'GMT+1', "HH:mm:ss"); and both display the correct time inside the Execution log and the cell.
If you test the code in a new Apps Script does it show the incorrect time zone too? Have you reviewed the time timezone inside Project Settings > General Settings?
If after all that you still have the same issue, I will recommend creating an issue tracker by following the steps here.
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.
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);
}
I am trying to find an app script solution to automatically import an Excel file into my Google Sheet each morning. I have found a number of examples on this forum as to how to import an Excel file however I have two challenges that I have not been able find a solution for:
The filename of the Excel sheet I need to import changes daily as it is an inventory report from the night prior and the filename is based on the date and time the report was run.
Only the first 10 of the 14 characters of the filename are predictable. The filename is always "IV" followed by the year, month, day and the time the report ran however the time that the report runs is not consistent. For example IV201907142308.xls means the report ran July 14, 2019 at 11:08pm however the night before the report ran at 11:04pm.
Possible workaround:
Instead of trying to import an Excel file from a specified folder by matching only part of the filename I could have another sheet that runs a script listing all files in a folder and the link to each file and have the import Excel file script refer to that list in order to find the link to the correct Excel file. I can create the list all files script but I am not sure how to have the import Excel file script refer to a link on the sheet in order to upload the correct Excel file.
How do I solve the problem?
Actually, you don't even need your file's name to import it. Since you mention the time can change and doesn't matter, I'm assuming only one file is generated per day.
If it's indeed the case, you can find it by it 'created date' by comparing it to yesterday's date.
function importYesterdayReport() {
var folders = DriveApp.getFoldersByName(/* your folder's name */);
var files = folders.next().getFiles();
//yesterday's date setup
var date = new Date();
date.setDate(date.getDate() - 1)
var yesterday = date.toDateString(); //because time won't be the same
//loop thru files to find yesterday's
while(files.hasNext()){
var file = files.next();
date = file.getDateCreated();
var fileDate = date.toDateString(); //because time won't be the same
if(fileDate === yesterday){
/* your import code */
}
}
}
getDateCreated() method
Date() JS ref
Other option id to search for your file title. Since the search criteria is 'contains' the fact that the hours change is not significant.
So you can replace the 'yesterday's date setup by :
var date = new Date();
var year = date.getFullYear();
var month = ("0"+(date.getMonth()+1)).slice(-2);
var day = date.getUTCDate();
and use this instead for var files : (will need to be after yesterday's date setup)
var searchCrit = 'title contains "IV'+year+month+day+'"'
var files = folders.next().searchFiles(searchCrit);
searchFiles() method
There is an option to write a script and bind it to a trigger. The question is, how to get the current time in the script body?
function myFunction() {
var currentTime = // <<???
}
use the JavaScript Date() object. There are a number of ways to get the time, date, timestamps, etc from the object. (Reference)
function myFunction() {
var d = new Date();
var timeStamp = d.getTime(); // Number of ms since Jan 1, 1970
// OR:
var currentTime = d.toLocaleTimeString(); // "12:35 PM", for instance
}
I considered with timezone in my Google Docs like this:
timezone = "GMT+" + new Date().getTimezoneOffset()/60
var date = Utilities.formatDate(new Date(), timezone, "yyyy-MM-dd HH:mm"); // "yyyy-MM-dd'T'HH:mm:ss'Z'"
my script for Google docs
Use the Date object provided by javascript. It's not unique or special to Google's scripting environment.
Anyone who says that getting the current time in Google Sheets is not unique to Google's scripting environment obviously has never used Google Apps Script.
That being said, do you want to return current time as to what? The script user's timezone? The script owner's timezone?
The script timezone is set in the Script Editor, by the script owner. But different authorized users of the script can set timezone for the spreadsheet they are using from File/Spreadsheet settings menu of Google Sheets.
I guess you want the first option. You can use the built in function to get the spreadsheet timezone, and then use the Utilities class to format date.
var timezone = SpreadsheetApp.getActive().getSpreadsheetTimeZone();
var date = Utilities.formatDate(new Date(), SpreadsheetApp.getActive().getSpreadsheetTimeZone(), "EEE, d MMM yyyy HH:mm")
Alternatively, get the timezone offset from UTC time using Javascript's date method, format the timezone, and pass it into Utilities.formatDate().
This requires one minor adjustment though. The offset returned by getTimezoneOffset() runs contradictory to how we often think of timezone. If the offset is positive, the local timezone is behind UTC, like US timezones. If the offset is negative, the local timezone is ahead UTC, like Asia/Bangkok, Australian Eastern Standard Time etc.
const now = new Date();
// getTimezoneOffset returns the offset in minutes, so we have to divide it by 60 to get the hour offset.
const offset = now.getTimezoneOffset() / 60
// Change the sign of the offset and format it
const timeZone = "GMT+" + offset * (-1)
Logger.log(Utilities.formatDate(now, timeZone, 'EEE, d MMM yyyy HH:mm');
The Date object is used to work with dates and times.
Date objects are created with new Date().
var date= new Date();
function myFunction() {
var currentTime = new Date();
Logger.log(currentTime);
}