[英]How to insert the current date time into a google sheet, respecting the sheet's timezone, using sheets api v4?
I've been trying to insert the current date time into a google sheet from sheets api v4 for hours now. 我一直在尝试将当前日期时间插入几个小时的sheets api v4中的Google工作表中。 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). Js date object
无法使用,因为Google表格使用了不同的纪元(显然,这取决于mac vs win)。 I have been unable to find any library that can create this google sheets/excel date serial number. 我一直找不到能创建此Google表格/ Excel日期序列号的任何库。 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). 如果我将Js日期对象格式化为字符串并将其发送到工作表,由于服务器位置(运行工作表api的地方)与用户位置(使用工作表的地方)的关系,时区不正确。
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 . 我考虑过将Js Date对象转换为用户的时区,但Google工作表不会使用CLDR吐出GMT
或UTC
值。 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. 电子表格的时区,采用CLDR格式,例如America / New_York。 If the time zone isn't recognized, this may be a custom time zone such as GMT-07:00. 如果无法识别时区,则可能是自定义时区,例如GMT-07:00。
If I send the cell updateRequest
using =NOW()
as the value the date time is correct. 如果我使用=NOW()
作为值发送单元格updateRequest
,则日期时间正确。 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. 因此,总而言之,我想让Google表格创建日期,因为它知道用户的时区。 Surely there is a way to just enter a date with sheets api that respects the users timezone right?? 当然,有一种方法可以只使用表格API输入尊重用户时区的日期? If not what are my options? 如果没有,我有什么选择?
After more searching around I found out that moment-timezone
can use the CLDR
time zone format to give a correct date string. 经过更多搜索后,我发现moment-timezone
可以使用CLDR
时区格式提供正确的日期字符串。
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. 因此,我采用了这种方法,将date time object
转换为工作表用户的时区,然后将数据发送到工作表。
Here's how I did it, (i've abstracted away all the auth
, jwt
, spreadsheetId
parts): 这是我的操作方法(我已经提取了所有的auth
, jwt
和spreadsheetId
jwt
部分):
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. 希望这将帮助其他人不要浪费自己的时间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.