简体   繁体   English

Google Script,如何使用正则表达式将一行从 Sheet1 复制到 Sheet2 而不是一个单元格?

[英]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.

相关问题 使用 Google App Script 将列从 Sheet1 复制到 Sheet2 并远程复制 - Copy columns from Sheet1 to Sheet2 & remote duplicates using Google App Script 如何获取单元格值(来自 Google 表单的 Sheet1,来自公式的 Sheet2)到谷歌文档模板 - How to get the cell values (Sheet1 from Google Form, Sheet2 from Formula) to google doc template 如何使用 Google 脚本从 1 个 Google 工作表中复制特定值并粘贴到另一个工作表? - How can I copy specific values from 1 Google Sheet and paste to another Sheet using Google Script? 如何使用 Google Sheet Script 将数据范围从一个工作表复制到另一个工作表 - How to Copy a data range from one sheet to another sheet using Google Sheet Script Google Script:根据值将行从一张纸复制到另一张纸 - Google Script: Copy row from one sheet to another depending on value 谷歌脚本将数据从工作表 1 复制到第一个空行工作表 2 - google script copy data from sheet 1 to first empty row sheet 2 Firebase | 谷歌表 | 应用脚本 | 如何将列中的数据而不是行中的数据从 Firebase 保存到 Google 表格? - Firebase | Google Sheet | App Script | How do I save the data in column instead of row from Firebase to Google Sheet? 如何使用谷歌表格应用程序脚本在单元格上添加评论 - How Can I add comment on a cell using google sheet app script 用于匹配和复制基于另一个相邻单元格的 Google 表格脚本 - Google sheet script to match and copy based from another adjacent cell 根据单元格值将行从一张纸复制到另一张纸 - Copy row from one sheet to another base on cell value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM