简体   繁体   English

使用Google Apps脚本获取工作表中包含数据的特定行为空的所有行

[英]get all rows in a sheet with data where a specific column is empty using Google Apps Scripts

Our team has a Google Form we use for intake. 我们的小组有一个Google表格供我们使用。 Once an intake is submitted I have a GAS script that will process it (create some files in Google Drive, send some e-mails, etc...). 提交录入文件后,我有一个GAS脚本将对其进行处理(在Google云端硬盘中创建一些文件,发送一些电子邮件等)。

Instead of triggering on the on form submit event of the Spreadsheet linked to the Form, I am using an hourly trigger. 我使用的是每小时触发一次,而不是触发链接到表单的电子表格的表单提交事件。 There is a specific reason for this that is out-of-scope for this question but I can explain if needed. 有一个特定的原因无法解决此问题,但我可以根据需要进行解释。

The Spreadsheet linked to the Form will eventually have thousands of rows. 链接到表单的电子表格最终将具有数千行。

My script will go through each unprocessed row. 我的脚本将遍历每个未处理的行。 I added a column on the Spreadsheet to track which rows have been processed. 我在电子表格上添加了一个列,以跟踪已处理的行。 When a row is processed some string is added to column A. For unprocessed rows, column A will be empty. 处理行时,会将某些字符串添加到列A。对于未处理的行,列A将为空。

I am trying to find the fastest and most efficient way to get all the unprocessed rows -- rows where column A is blank. 我正在尝试找到最快和最有效的方法来获取所有未处理的行-A列为空白的行。

Initial thoughts: 最初的想法:

  • Use Sheet.getDataRange().getValues() to get all of the rows, iterate through each, and process the unprocessed ones. 使用Sheet.getDataRange().getValues()来获取所有行,遍历每行,并处理未处理的行。 Then use the index of the row to mark the row processed on the Spreadsheet. 然后使用该行的索引来标记在电子表格上处理的行。 This way would work but after the Spreadsheet has thousands of rows this way will get really slow. 这种方法可以工作,但是在电子表格具有数千行之后,这种方法将变得非常慢。
  • Apply a filter to the Spreadsheet and filter on rows where column A is blank. 对电子表格应用过滤器,并在A列为空白的行上进行过滤。 I can apply the filter but then there doesn't seem to be an easy way to get all the visible rows and their row number so I can mark them processed. 我可以应用过滤器,但是似乎没有一种简单的方法来获取所有可见行及其行号,因此我可以将它们标记为已处理。

So then I figured out I could use the query function on a second sheet to show all rows where column A is empty and then I could use that sheet in my script for what needs to be processed. 因此,我发现可以在第二张纸上使用query功能来显示A列为空的所有行,然后可以在脚本中使用该纸来处理需要处理的内容。

=query('Form Responses'!A:V, "SELECT * WHERE A IS NULL")

But this does not include the row number from the Form Responses sheet so I wouldn't know which row to go back to on Form Responses to mark processed. 但这不包括“ Form Responses表中的行号,因此我不知道要在“ Form Responses上返回哪一行以标记已处理。

I was able to add the row number using arrayformula like this: 我能够使用arrayformula添加行号,如下所示:

=query({arrayformula(row('Form Responses'!A:V)), 'Form Responses'!A:V}, "SELECT * WHERE Col2 IS NULL")

But this adds a bunch of rows with numbers, even if the source table doesn't have data in that row. 但这会添加一堆带有数字的行,即使源表在该行中没有数据也是如此。

And if I don't include the row number it still wont work cause if I call Sheet.getDataRange() on the sheet that has the query function, it returns more rows than there is data. 而且,如果我不包括行号,那么如果我在具有query功能的工作表上调用Sheet.getDataRange() ,它仍然无法正常工作,它返回的行多于数据。

I made an example sheet - https://docs.google.com/spreadsheets/d/1LYpqfj3clZiaGCPgp_eRfrxhQKWambRyQn3uNCFAIpU/edit?usp=sharing . 我制作了一个示例表-https: //docs.google.com/spreadsheets/d/1LYpqfj3clZiaGCPgp_eRfrxhQKWambRyQn3uNCFAIpU/edit?usp=sharing

So is there a fast/efficient way to get all rows with data in a sheet where a specific column is empty? 那么,是否有一种快速/有效的方法来获取表中特定列为空的所有行以及数据? Either using query or some other way? 使用query还是其他方式?

For a purely Apps Script answer to finding the position of a value within your sheet, either TextFinder (I'm not sure if this would be appropriate for empty fields) or getValues() and a loop ought to be fast enough. 对于纯粹的Apps脚本答案,可以在工作表中查找值的位置,请使用TextFinder (我不确定这是否适合空字段)或getValues()以及循环应该足够快。 You might consider sorting the sheet prior to using getValues() , or sorting the array after, depending on the value you're seeking, or eg starting your loop from the most recent entries if you expect to be looking for newer items. 您可能会考虑在使用getValues()之前对工作进行排序,或者在之后对数组进行排序,这取决于要查找的值,或者例如,如果您希望查找较新的项,则从最新条目开始循环。

Alternatively, however, if you are using Sheets' formulas, you can easily add =row() to a column (or eg =arrayformula(row(A2:A)) ) such that the results of =query return a row number. 但是,或者,如果您使用的是Sheets的公式,则可以轻松地将=row()添加到列(例如=arrayformula(row(A2:A)) ),以使=query的结果返回行号。 If it's not feasible to add this column to the original sheet for whatever reason, it may also work to use match with the query result as the search key and the original sheet as the range, depending on how your spreadsheet is structured. 如果出于任何原因将此列添加到原始工作表都不可行,则可以将match结果与查询结果作为搜索关键字,将原始工作表用作范围,这取决于电子表格的结构。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Google Apps 脚本从 Google 电子表格中选择特定列中的值不为空的行 - Select the rows from a Google Spreadsheet where values in a certain column are not empty using Google Apps Scripts Google Sheets + Apps Scripts,从一个工作表复制/粘贴到另一个工作表,但粘贴到特定列 (B) 中的第一个空单元格 - Google Sheets + Apps Scripts, Copy/Paste from one sheet to another, but Paste into first empty cell in specific column (B) Google Apps Scripts / Google Sheet - 禁用所有活动过滤器,显示特定单元格并根据条件再次过滤 - Google Apps Scripts / Google Sheet - Disable all the active filters, Show a specific cell and filter again based on criteria 如何使用 Google Apps 脚本从 JSON 创建数组并将值导入到 Google 表格中的多行 - How to Create an Array From JSON and Import the Values to Multiple Rows in a Google Sheet Using Google Apps Scripts 删除 Google Apps 脚本中特定列中的所有空格 - Delete all empty spaces in specific column in Google Apps Script 我无法使用谷歌应用脚本隐藏谷歌表格的最后一列 - I can't hide the last column of a google sheet using google apps scripts Google Apps 脚本 - 格式化特定工作表中的列 - Google Apps Script - Format Column in Specific Sheet 使用谷歌应用程序脚本对列函数中的所有行求和 - Sum all rows in a column function using google app scripts Google脚本-当H列具有特定颜色(黄色)时,将行复制到新工作表 - Google Scripts - Copy rows to a new sheet when column H has a specific colour (Yellow) 使用Google Apps脚本对行进行分组 - Group rows using google apps scripts
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM