[英]What's the best way to unify date formats after retrieving them from cells and ranges in Google Sheets to Apps Script?
Goal: I need to iterate through rows and match the values within column 'A2:A' with cell 'C2', and then console.log only the matching values from the column.目标:我需要遍历行并将'A2:A'列中的值与单元格'C2'匹配,然后console.log仅匹配列中的值。
Problem: The values in 'C2' and 'A2:A' are set to be in 'MM/dd/yyyy' format in Google Sheets.问题: “C2”和“A2:A”中的值在 Google 表格中设置为“MM/dd/yyyy”格式。 But as I try to retrieve them via Apps Script, sheet1.getRange('C2).getValue() seems to return a different date format than sheet2.getRange('A2:A').getValues() (see snippet comment), so the 'if' statement failed to find any matching value.
但是,当我尝试通过 Apps 脚本检索它们时, sheet1.getRange('C2).getValue() 似乎返回的日期格式与 sheet2.getRange('A2:A').getValues() 不同(参见代码段注释),所以'if'语句找不到任何匹配的值。
What I need help with: Coming up with the best way to unify the date formats after retrieving the values such that the 'if' statement can recognize them and validate the condition.我需要帮助:在检索值后提出统一日期格式的最佳方法,以便“if”语句可以识别它们并验证条件。
The snippet:片段:
function matchDates() { var ss = SpreadsheetApp.getActiveSpreadsheet(); var database = ss.getSheetByName("Database"); var form = ss.getSheetByName("Form"); var formDate = form.getRange("C2").getValue(); // Console.log returns: 'Fri Jan 07 2022 12:00:00 GMT-0500 (Eastern Standard Time)' var dbDate = database.getRange("A2:A").getValues(); // Console.log returns: [ Sat Jan 08 2022 12:00:00 GMT-0500 (Eastern Standard Time) ],[ Fri Jan 07 2022 12:00:00 GMT-0500 (Eastern Standard Time) ],.. for(let i = dbDate.length - 1; i >= 0; i--){ if(dbDate[i] == formDate){ console.log(i+1,dbDate[i]) // Console.log all rows in 'Date' column of 'Database' sheet that match the value stored in formDate } } } // Console.log this whole function returns nothing because the 'if' statement cannot find any matching dates (due to the stated problem).
Research results so far:迄今为止的研究成果:
var formDate = form.getRange('C2').getDisplayValue(); // Console.log returns 1/8/2022 var dbDate = database.getRange('A2:A').getDisplayValues(); // Console.log returns [ '1/8/2022' ],[ '1/7/2022' ],..
Using Utilities.formatDate() - Only works for 'C2', even then the value returned by console.log is 1 day behind what's shown on the sheet.使用 Utilities.formatDate() - 仅适用于“C2”,即使这样,console.log 返回的值也比工作表上显示的值晚 1 天。 I don't know how to format an entire column of 'A2:A' using this because doing it the same way returned me only a single row while it should be the whole range.
我不知道如何使用它来格式化“A2:A”的整个列,因为以同样的方式执行它只返回了一行,而它应该是整个范围。
Using new Date() as suggested by @Cooper in the comment.按照@Cooper 在评论中的建议使用 new Date() 。 Again, worked for 'C2', but not for 'A2:A'.
同样,适用于“C2”,但不适用于“A2:A”。 Snippet;
片段;
var formDate = new Date(form.getRange("C2").getValue()).valueOf(); // Returns the date in this format: 1641574800000 var dbDate = new Date(database.getRange("A2:A").getValues()).valueOf(); //Returns NaN
I'm a complete newbie and running out of references for this so please, really appreciate the help!我是一个完整的新手,并且没有参考资料,所以请,非常感谢您的帮助!
Transform your dates in epoch individually单独转换您在纪元中的日期
var dbDate = [[ 'Sat Jan 08 2022 12:00:00 GMT-0500 (Eastern Standard Time)' ],[ 'Fri Jan 07 2022 12:00:00 GMT-0500 (Eastern Standard Time)' ]] var dbDateEpoch = [] dbDate.forEach(function(d){ dbDateEpoch.push(new Date(d).valueOf().toString()) }) console.log(dbDateEpoch)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.