简体   繁体   English

比较两个列中的两列A,并使用Google Apps脚本在表单1中生成唯一值

[英]Compare two columns A in two sheets and generate unique values in sheet 1 using Google Apps Script

I have two sheets with a dozen of columns and thousands of rows. 我有两张纸,上面有十几列和几千行。 Each sheet has a column A that contains a unique ID. 每个工作表都有一个包含唯一ID的列A. I want to compare two sheets using only the column A as a point of comparison and generate a third sheet that contains entire rows with the unique ID that is in Sheet 1 but not in Sheet 2. 我想比较仅使用A列作为比较点的两张纸,并生成第三张纸,其中包含具有Sheet 1中唯一ID但不在Sheet 2中的唯一ID的整个行。

Here is a visualisation: 这是一个可视化:

Sheet 1
+---+--------+-----+-----+
|   |   A    |  B  |  C  |
+---+--------+-----+-----+
| 1 | 1111   | xxx | zzz |
| 2 | 2222   | yyy | zzz |
| 3 | 2222-1 | zzz | xxx |
| 4 | 3333   | xxx | yyy |
+---+--------+-----+-----+
Sheet 2
+---+------+-----+-----+
|   |  A   |  B  |  C  |
+---+------+-----+-----+
| 1 | 1111 | xxx | zzz |
| 2 | 2222 | yyy | zzz |
| 3 | 3333 | xxx | yyy |
+---+------+-----+-----+

The desired function would compare Sheet 1 and Sheet 2 using A as the basis and the function would point out that the cell sheet1[A2] is unique, it would copy the entire 2:2 row, and then it would paste it into a newly generated sheet. 所需的函数将使用A作为基础比较表1和表2,函数将指出单元格sheet1 [A2]是唯一的,它将复制整个2:2行,然后它将它粘贴到一个新的生成的表格。 The content of B and C is irrelevant in comparison. 相比之下,B和C的含量无关紧要。

I wanted to create a loop that would go and compare each cell in column A for both sheets and if sheet1.A[n] != sheet2.A[n], but it would work only if both sheets had the same lengths. 我想创建一个循环来比较两个工作表中A列中的每个单元格,如果是sheet1.A [n]!= sheet2.A [n],但只有当两个工作表具有相同的长度时它才会起作用。

In the case I've specified above, the loop I've created with the help of the user Cooper finds that row 3 is unique and after that everything will be unique because of a misalignment. 在我上面指定的情况下,我在用户Cooper的帮助下创建的循环发现第3行是唯一的,之后由于未对齐,所有内容都将是唯一的。

The sheets will always be of different lengths and the uniques may appear at different spots (ie 2222-1 may end up the next time I'm making a comparison at row 5). 纸张将始终具有不同的长度,并且唯一身份可能出现在不同的位置(即,2222-1可能在下一次我在第5行进行比较时结束)。

function compareSheetDrop(input) {

var ss = SpreadsheetApp.getActiveSpreadsheet();

//establish first sheet and get its data range
var dropSheet = ss.getSheetByName("Drop (2)");
var dropRange = dropSheet.getRange(2, 1, dropSheet.getLastRow() - 1, dropSheet.getLastColumn());
var vA1 = dropRange.getValues();

//establish the sheet with new data and get its data range
var compareSheet = ss.getSheets()[4];
var compareRange = compareSheet.getRange(2, 1, compareSheet.getLastRow() - 1, compareSheet.getLastColumn());
var vA2 = compareRange.getValues();

//establish the sheet with results
var resultSheet = ss.getSheetByName("ADQA");

for (var i = 0; i < vA1.length; i++) {

//i've tried to make the loop stop once it encounters a blank cell to avoid a type error, but doesn't work
    if (vA2[i][0] === '' || vA1[i][0] === '') {
        break;
        }

    else if (vA1[i][0] != vA2[i][0]) {
    Logger.log(vA1[i][0] + " & " + vA2[i][0]);
            resultSheet.appendRow(vA2[i]);
        }
    }
}

Even if my code doesn't provide results I want, there are also two problems with it: It loops until there's a TypeError: Cannot read property "0" from undefined. 即使我的代码没有提供我想要的结果,它也有两个问题:它循环直到出现TypeError: Cannot read property "0" from undefined. that I've tried to combat but to no avail. 我试图打击但无济于事。

Moreover, the appendRow method doesn't write data in already existing cells but inserts the row before the already existing ones - after running my test script 3 times the results sheet had thousands of columns and was barely operable. 此外, appendRow方法不会在现有单元格中写入数据,而是在已存在的单元格之前插入行 - 在运行我的测试脚本3次后,结果表有数千列并且几乎无法操作。

Any ideas how I could modify my existing code to get what I want while avoiding the aforementioned issues? 任何想法如何修改我现有的代码以获得我想要的,同时避免上述问题? Or does it require a completely new approach? 还是需要一种全新的方法? Any suggestions? 有什么建议么?

Flow: 流:

  • Use Array#map to get all of sheet1 col A in a array(say a ). 使用Array#map获取Array#map所有sheet1 col A(比如说a )。
  • Use Array#filter to filter sheet2 by the array above. 使用Array#filter按上面的数组过滤sheet2。
  • Use the filtered array directly in setValues() 直接在setValues()中使用过滤后的数组

Snippet: 片段:

 var a1 = [[1,'x','z'],[2,'y','z'],[3,'m','z'],['2-1','x','z']];//sheet1ValuesArr var a2 = [[1,'x','z'],[2,'y','z'],[3,'m','z']];//Sheet2ValuesArr var a = a2.map(function(e){ return e[0]}); //Sheet2Col A : [1,2,3] var filteredArr = a1.filter(function(e){return !~a.indexOf(e[0])}); console.log(filteredArr); //to use in setValues 

resultSheet.getRange(1, 1, filteredArr.length, filteredArr[0].length).setValues(filteredArr)

References: 参考文献:

暂无
暂无

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

相关问题 Google Apps 脚本 - 按列名而不是硬编码范围比较两张表的更改 - Google Apps Script - Compare two sheets for changes by column names instead of hardcoded ranges 串联Google表格脚本中的两列 - Concatenate two columns in google sheets script 比较两个Google表格并找到唯一的值(仅在其中一个表格上) - Comparing two Google Sheets and finding unique values (only on one of the sheets) 通过搜索/比较自动增加值并匹配 Google 表格脚本中的两列 - Automatically increment value by search /compare and match two columns in Google Sheets Script 使用Google Apps脚本自动生成Google表格的参考号 - Generate Reference Number to Google Sheets automatically using Google Apps Script 在两张不同的纸上比较 arrays 并只取唯一值 - Compare arrays on two different sheets and take only unique values 在比较两张表中的两列时,无法复制多个值以获取Google Apps脚本中的差异 - Can't copy more than one value when comparing two columns in two sheets for differences in Google Apps Script Google表格脚本:在两个值之间获取值? - Google Sheets Script: Getting values between two values? 比较 Google 表格中两列数据和 output 只有使用 Google 脚本的不匹配数据的最佳方法是什么? - What's the best way to compare two columns of data in Google Sheets and output ONLY the non-matching using Google Scripts? 如何使用脚本从 Google 表格中“工作表 B”上的数据生成“工作表 A”上的列表 - How Do You Generate List On "Sheet A" from Data on "Sheet B" in Google Sheets Using a Script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM