[英]Google Script, how can I copy a row from Sheet1 to Sheet2 instead of just one cell by using regex?
Extremely in-experienced coder here, I have been working on a regex function that searches for keywords in transcripts, then populates a spreadsheet with a transcript if the keywords are found.这里经验丰富的编码员,我一直在研究一个正则表达式函数,该函数在成绩单中搜索关键字,然后在找到关键字时用成绩单填充电子表格。 However, I have only worked out how to populate the sheet with only the one cell, containing the transcript, whereas I want to populate the entire row in which has the transcript cell as well as other cells containing urls, date and times, etc.
但是,我只研究了如何仅使用包含转录本的一个单元格填充工作表,而我想填充包含转录本单元格以及其他包含 url、日期和时间等单元格的整行。
Here is my code:这是我的代码:
function regexwithsheetpop() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var historySheet = ss.getSheetByName('sheet1');
var resultsSheet = ss.getSheetByName('Results');
var totalRowsWithData = historySheet.getDataRange().getNumRows();
var data = historySheet.getRange(1, 2, totalRowsWithData).getValues();
var regexp = /\W*(identity)\W*\s+(\w+\s+){0,5}(verification)|(verification)\s+(\w+\s+){0,5}(identity)/i;
var result = [];
for (var i = 0; i < data.length; i += 1) {
var row = data[i];
var column = row[0];
if (regexp.exec(column) !== null) {
result.push(row); }}
if (result.length > 0) {
var resultsSheetDataRows = resultsSheet.getDataRange().getNumRows();
resultsSheetDataRows = resultsSheetDataRows === 1 ? resultsSheetDataRows : resultsSheetDataRows + 1;
var resultsSheetRange = resultsSheet.getRange(resultsSheetDataRows, 1, result.length);
resultsSheetRange.setValues(result);}}
So I search Column B in "Sheet1", which contains all the transcripts, for keywords.因此,我在包含所有成绩单的“Sheet1”中的 B 列中搜索关键字。 And if a keyword is found, the regex function populates the "Results" sheet with the transcripts.
如果找到关键字,regex 函数会用成绩单填充“结果”表。 I would like it if the regex function populates the "Results" sheet with Column A, B and C of "Sheet1" if there is a keyword found in Column B of "Sheet1".
如果在“Sheet1”的 B 列中找到关键字,正则表达式函数将“Sheet1”的 A、B 和 C 列填充到“结果”表中。
Again, very in-experienced coder here so any simplified or dumb-down feedback would be greatly appreciated.再次,非常有经验的编码员,因此任何简化或愚蠢的反馈都将不胜感激。 Thanks
谢谢
Welcome!欢迎! You're most of the way there.
你大部分时间都在那里。 To pull in the additional columns from "sheet1", you simply need to include an extra argument in
getRange()
to get all 3 columns (A, B, C):要从“sheet1”中提取额外的列,您只需在
getRange()
包含一个额外的参数即可获取所有 3 列(A、B、C):
var data = historySheet.getRange(1, 1, totalRowsWithData, 3).getValues()
See getRange(row, column, numRows, numColumns)
.请参阅
getRange(row, column, numRows, numColumns)
。
Then tweak your for-loop to match the regex on column B and push the entire row
array onto results
if a match is found.然后调整您的 for 循环以匹配 B 列上的正则表达式,如果找到匹配
results
则将整个row
数组推送到results
。
More about multi-dimensional range outputs here: How do I write data to a single range in Google Apps Script?在此处详细了解多维范围输出: 如何在 Google Apps 脚本中将数据写入单个范围?
And finally, add another argument for the number of columns for resultsSheetRange
.最后,为
resultsSheetRange
的列数添加另一个参数。
function regexwithsheetpop() {
var ss = SpreadsheetApp.getActiveSpreadsheet()
var historySheet = ss.getSheetByName('sheet1')
var resultsSheet = ss.getSheetByName('Results')
var totalRowsWithData = historySheet.getDataRange().getNumRows()
var data = historySheet.getRange(1, 1, totalRowsWithData, 3).getValues()
var regexp = /\W*(identity)\W*\s+(\w+\s+){0,5}(verification)|(verification)\s+(\w+\s+){0,5}(identity)/i
var result = []
for (var i = 0; i < data.length; i += 1) {
var row = data[i]
var column = row[1]
if (regexp.exec(column) !== null) {
result.push(row)
}
}
if (result.length > 0) {
var resultsSheetDataRows = resultsSheet.getDataRange().getNumRows()
resultsSheetDataRows =
resultsSheetDataRows === 1
? resultsSheetDataRows
: resultsSheetDataRows + 1
var resultsSheetRange = resultsSheet.getRange(
resultsSheetDataRows,
1,
result.length,
3
)
resultsSheetRange.setValues(result)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.