简体   繁体   English

如果值存在于工作表 1 和工作表 2 中,则复制行并将其粘贴到工作表 3。Google 表格。 Google Apps 脚本

[英]Copy row if value exists in both sheet 1 and sheet 2 and paste it to sheet 3. Google Sheets. Google Apps Script

I am needing help on a part of my script.我的脚本的一部分需要帮助。 I have a large imported list of data on sheet one, on one column there are a list of phone numbers.我在第一张纸上有大量导入的数据列表,其中一列有电话号码列表。 I want to compare that column of phone numbers to a set list of numbers in sheet 2. If there are matching numbers, copy the entire row from sheet 1 to sheet 3. Here is what i have so far.我想将该列电话号码与工作表 2 中的一组数字列表进行比较。如果有匹配的数字,请将整行从工作表 1 复制到工作表 3。这是我目前所拥有的。

function copyRowtoSheet3() { 
  var s1 = SpreadsheetApp.openById("1Aw11LiKzyezfrTQIuTsJPhUFtz8RPqLCc8FlIiy0ZlE").getSheetByName('Sheet1');
  var s2 = SpreadsheetApp.openById("1Aw11LiKzyezfrTQIuTsJPhUFtz8RPqLCc8FlIiy0ZlE").getSheetByName('Sheet2'); 
  var s3 = SpreadsheetApp.openById("1Aw11LiKzyezfrTQIuTsJPhUFtz8RPqLCc8FlIiy0ZlE").getSheetByName('Sheet3'); 
  var values1 = s1.getDataRange().getValues();
  var values2 = s2.getDataRange().getValues();
  var resultArray = [];
  for(var n=0; n < values1.length ; n++){
    var keep = false;
    for(var p=0; p < values2.length ; p++){
      Logger.log(values1[n][0]+' =? '+values2[p][0]);
      if( values1[n][0] == values2[p][0] && values1[n][1] == values2[p][1]){
        resultArray.push(values1[n]);
        Logger.log('true');
        //break ;
      }
    }
  }  
  s3.getRange(+1,1,resultArray.length,resultArray[0].length).setValues(resultArray);
}

What i have now kindof works, however its not perfect as it only seems to copy the number from sheet one, not the entire row, or doesnt work at all.我现在所拥有的有点有效,但是它并不完美,因为它似乎只是从第一张纸中复制数字,而不是整行,或者根本不起作用。 I get this error a lot, but not all the time.我经常收到此错误,但并非总是如此。 TypeError: Cannot read property 'length' of undefined (line 19, file "Code") Not quite sure what im doing wrong.类型错误:无法读取未定义的属性“长度”(第 19 行,文件“代码”)不太确定我做错了什么。 Any advice here would be helpful.这里的任何建议都会有所帮助。 For reference, Here is what my sheets look like and what im wanting this code to do.作为参考,这是我的工作表的外观以及我希望此代码执行的操作。

Sheet1
Name      Phone Number  
John      2142354696
Virge     2345678901
Jason     2412305968
Tiffany   1304086932

Sheet2
Phone Numbers
2142354696
2412305968


Sheet3
John      2142354696
Jason     2412305968

It works only when the entire row is similar, because in If you are comparing the value of the two columns of the two worksheets.它的工作原理,只有当整个行是类似的,因为If你是比较两个工作表的两列的值。

If in sheet1 the phone number is in the second column and in sheet2 the number is in the first column, the correct thing would be to compare sheet1 [row N] [column 1] == sheet2 [row N] [column 0].如果在 sheet1 中电话号码在第二列中,而在 sheet2 中电话号码在第一列中,正确的做法是比较 sheet1 [row N] [column 1] == sheet2 [row N] [column 0]。 This is the only comparison to be made, only the cells of interest.这是唯一要进行的比较,只有感兴趣的细胞。

Keep all your code and just change the If line to:保留所有代码,只需将If行更改为:

if( values1[n][1] == values2[p][0] ){

暂无
暂无

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

相关问题 将工作表 1 与工作表 2 进行比较并输出至工作表 3。Google 工作表。 JavaScript - Compare sheet 1 to sheet 2 and output to sheet 3. Google sheets. JavaScript Google脚本:如何将范围复制并粘贴到不为空的行中的新表中 - Google Script: How to copy and paste range to a new sheet on a row that is not empty 通过for循环谷歌表格脚本将行复制到另一张表格 - Copy row to another sheet via for loop google sheets script 从工作表 A 复制粘贴到工作表 B 的最后一行谷歌应用程序脚本 - Copy fro Sheet A to paste in Sheet B's last row google app script Google表格将行移动到新表格的脚本 - Script for Google Sheets to Move Row to New Sheet 将工作表复制到另一个电子表格[Google Apps脚本] - Copy sheet to another spreadsheet [Google Apps Script] 在谷歌工作表中搜索工作表名称时的通配符表达式。 谷歌应用脚​​本 - Wildcard expression when searching for sheet name in google sheets. Google App Script 为每一行谷歌工作表循环一个应用程序脚本 - Looping an apps script for every row of google sheet Google脚本:如果另一个工作表中存在该行的值,则删除该行 - Google Script: Delete row if a value in it exists in another sheet 将源工作表中的数据与目标工作表进行比较,并使用 Google Apps 脚本将缺失的行复制到目标工作表 - Compare data in source sheet with target sheet and copy missing rows to target sheet with Google Apps Script
 
粤ICP备18138465号  © 2020-2025 STACKOOM.COM