Reputation: 1195
I've been trying to insert the current date time into a google sheet from sheets api v4 for hours now. I can't believe how difficult this is to figure out!
So far I've found that:
Js date object
won't work as google sheets uses a different epoch (one that apparently changes depending on mac vs win). I have been unable to find any library that can create this google sheets/excel date serial number. If I could that might work.
If I format the Js date object as a string and send it to the sheet, the timezone is incorrect due to server location (where sheets api is running) vs user location (where the sheet is being used).
I considered converting the Js Date object to the user's timezone but google sheets doesn't spit out a GMT
or UTC
value it uses CLDR. So far I haven't found anyway to use that information to convert the timezone.
The time zone of the spreadsheet, in CLDR format such as America/New_York. If the time zone isn't recognized, this may be a custom time zone such as GMT-07:00.
If I send the cell updateRequest
using =NOW()
as the value the date time is correct. But it updates every time the spreadsheet is edited ... which is unusable as a timestamp for a row submission. Wish you could turn that off!
So in summary, I would like to let google sheets create the date since it knows about the timezone of the user. Surely there is a way to just enter a date with sheets api that respects the users timezone right?? If not what are my options?
Upvotes: 0
Views: 2090
Reputation: 1195
After more searching around I found out that moment-timezone
can use the CLDR
time zone format to give a correct date string.
So I've gone with this method, converting the date time object
to the sheets user's time zone and then sending the data to the sheet.
Here's how I did it, (i've abstracted away all the auth
, jwt
, spreadsheetId
parts):
const moment = require('moment-timezone');
const {google} = require('googleapis');
const sheets = google.sheets('v4');
const getSpreadsheetProperties = (jwt, sheets,spreadsheetId) => {
try{
var request = {
spreadsheetId: spreadsheetId,
ranges: [],
includeGridData: false,
auth: jwt,
};
return new Promise ((resolve, reject) => {
sheets.spreadsheets.get(request, (err, response) => {
if (err) {
console.error('getSpreadsheetProperties - error', err);
reject(false)
}
console.log('getSpreadsheetProperties - response', response)
resolve(response)
});
})
} catch(error) {
console.error('getSpreadsheetProperties - error', error)
return error
}
}
const getSheetProperties = (spreadsheetProperties) => {
return spreadsheetProperties.data.sheets
}
const getTimeZone = (spreadsheetProperties) => {
return spreadsheetProperties.data.properties.timeZone
}
const getDate = (timeZone) => {
const date = moment.tz(timeZone).format('DD-MM-YY, h:mm A')
return date
}
const ssProps = await getSpreadsheetProperties(jwt, sheets,spreadsheetId)
const timeZone = getTimeZone(ssProps)
const date = getDate(timeZone)
Hopefully this will help others not waste hours of their life.
Upvotes: 2