[英]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: 最初的想法:
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. 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.