简体   繁体   English

从 Google 表格中的单元格和范围检索到 Apps 脚本后,统一日期格式的最佳方法是什么?

[英]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:迄今为止的研究成果:

  1. Use getDisplayValue() instead of getValue() - Still didn't work for the 'if' statement.使用 getDisplayValue() 而不是 getValue() - 仍然不适用于“if”语句。 The display value retrieved from 'C2' is different from 'A2:A', something to do with array dimension (red it somewhere but lost the reference), as shown here;从'C2'检索的显示值与'A2:A'不同,与数组维度有关(红色某处但丢失了引用),如下所示;

 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' ],..

  1. 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”的整个列,因为以同样的方式执行它只返回了一行,而它应该是整个范围。

  2. 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单独转换您在纪元中的日期

valueOf() 的价值()

 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.

相关问题 无法从Google Apps脚本的多个工作表中获取范围 - Cannot get ranges from multiple sheets in google apps script 有没有办法将 onEdit function 应用于谷歌表格应用程序脚本中的多个单独范围? - Is there a way to apply an onEdit function to multiple separate ranges in google sheets Apps Script? 在谷歌应用程序脚本中解析 html 的最佳方法是什么 - What is the best way to parse html in google apps script Google Apps 脚本 - 更改一系列 google 表格单元格中的值 - Google Apps Script - Changing values in a range of google sheets cells 如何从 Google 表格中获取数据并在 Google Apps 脚本中处理它们? - How to get data from Google Sheets and handle them in Google Apps script? Google Apps脚本未从电子表格中检索信息 - Google Apps Script not retrieving information from spreadsheet 从 Apps 脚本显示消息 Google 表格 - Display message Google Sheets from Apps Script Google应用脚本可停止特定日期范围内的触发器 - Google apps script to stop the trigger on specific date ranges 在 Google 表格中使用 Google Apps 脚本时如何防止日期变成字符串? - How to keep the date from turning into a String when using a Google Apps Script in Google Sheets? 从各种 Google 表格单元格中提取不同的链接,使用 Google Apps 脚本将它们作为活动 URL/超链接粘贴到 Google 文档模板中 - Extract different links from various Google Sheet cells, paste them in a Google Doc Template as Active URLs /Hyperlinked using Google Apps Script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM