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