在 App 腳本、谷歌表中運行。我從 API 獲取時間戳并回傳此結果:
[[1.6370611672429312E18], [1.63706107263277082E18]],
我做了這段代碼并且作業正常,但問題不在陣列中,我每次都需要它在陣列中:
const timestamps = [[1.6370611672429312E18], [1.63706107263277082E18]];
for (let i = 0; i < timestamps.length; i ) {
const timestamp = new Date(timestamps[i][0] / 1000000);
const convertDate = Utilities.formatDate(timestamp, "GMT", "MM-dd-yyyy HH:mm:ss");
}
結果是:
1:52:34 PM Info 11-16-2021 12:52:33
1:52:34 PM Info 11-16-2021 12:52:33
1:52:34 PM Info 11-16-2021 12:52:33
我需要這樣的結果: 1:52:34 PM Info [[11-16-2021 12:52:33], [11-16-2021 12:50:10]]
通過此代碼放入作業表: sheet.getRange(1,1,convertDate .length, convertDate [0].length).setValues(convertDate );
uj5u.com熱心網友回復:
const timestamps = [[1.6370611672429312E18], [1.63706107263277082E18]];
const toISOStringWithTimezone = date => {
const pad = n => `${Math.floor(Math.abs(n))}`.padStart(2, '0');
return date.getFullYear()
'-' pad(date.getMonth() 1)
'-' pad(date.getDate())
' ' pad(date.getHours())
':' pad(date.getMinutes())
':' pad(date.getSeconds())
};
const hasArrDate = (data = []) => {
return data.flat().map(v => toISOStringWithTimezone(new Date(v / 1000000)))
}
console.log(hasArrDate(timestamps));
uj5u.com熱心網友回復:
const timestamps = [[1.6370611672429312E18], [1.63706107263277082E18]];
let convertedDates = []; // Create an array to store the converted dates
for (let i = 0; i < timestamps.length; i ) {
const timestamp = new Date(timestamps[i][0] / 1000000);
const convertedDate = Utilities.formatDate(timestamp, "GMT", "MM-dd-yyyy HH:mm:ss");
convertedDates.push([convertedDate]); // Wrap each in [] because this will be printed as a single column
}
const sheet = SpreadsheetApp.openById(SPREADSHEET_ID).getSheetByName(SHEET_NAME);
sheet.getRange(1, 1, convertedDates.length, convertedDates[0].length).setValues(convertedDates);
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/357447.html
