简体   繁体   English

Apps 脚本 - 比较两个 arrays,找出差异(单元格的值和索引)

[英]Apps Script - compare two arrays, find differences (value and index of cells)

I have two vertical 1D arrays, one with existing data, and another that could (potentially) have differences.我有两个垂直的 1D arrays,一个具有现有数据,另一个可能(可能)存在差异。

I want to be able to compare the two arrays, find the differences AND the index of each cell that is different.我希望能够比较两个 arrays,找到不同之处以及每个不同单元格的索引。

Then, I would use that information to find the appropriate cells in the existing data that need to be changed, and set the new values in those specific cells, without having to copy and paste the entire array.然后,我将使用该信息在现有数据中找到需要更改的适当单元格,并在这些特定单元格中设置新值,而无需复制和粘贴整个数组。

For example:例如:

Existing Data现有数据 New Data新数据
John Smith约翰·史密斯 John Smith约翰·史密斯
012345 012345 012345 012345
6th grade六年级 7th grade 7年级
555-1234 555-1234 555-1357 555-1357
Trumpet喇叭 Trumpet喇叭
5th period第五期 2nd period第二期
Jane Smith简·史密斯 Jane Smith简·史密斯
js@email.com js@email.com js@email.com js@email.com

In this case, the code would see that rows 3,4, and 6 have differences, save those new values and their places in the array, then update the appropriate values in the main data list without changing anything else.在这种情况下,代码将看到第 3、4 和 6 行存在差异,将这些新值及其在数组中的位置保存,然后在主数据列表中更新适当的值而不更改任何其他内容。

I've tried multiple ways to compare the two arrays AND get the index of the rows that have differences and this is as far as I've gotten without an error or a 'null' result:我尝试了多种方法来比较两个 arrays 并获取有差异的行的索引,这是我没有错误或“空”结果的情况:

function updateInfo() {

var ss = SpreadsheetApp.getActiveSpreadsheet();
var currentSheet = ss.getSheetByName("CURRENT");
var infoSheet = ss.getSheetByName("INFO Search");

var origVal = infoSheet.getRange(5,2,52,1).getValues();
var newVal = infoSheet.getRange(5,4,52,1).getValues();
var list = [];

var origData = origVal.map(function(row,index){
  return [row[0],index];
});
 Logger.log(origData);

var newData = newVal.map(function(row,index){
  return [row[0],index];
});
Logger.log(newData);

This just gives me the value and index of each cell.这只是给了我每个单元格的值和索引。 Is there a fast and efficient way to compare the two arrays, get the data I need, and change the values in just certain cells of the original column?有没有一种快速有效的方法来比较两个 arrays,获取我需要的数据,并更改原始列的某些单元格中的值? I can't just copy and paste the whole column over because there are formulas embedded in various rows that need to remain intact.我不能只是复制和粘贴整个列,因为在需要保持完整的各行中嵌入了公式。

Thanks for any help you can provide!感谢您的任何帮助,您可以提供!

Solution:解决方案:

Iterate through the new data array.遍历新的数据数组。 For each value, check whether the new value matches the old one.对于每个值,检查新值是否与旧值匹配。 If that's the case, add the corresponding value and index to your list .如果是这种情况,请将相应的值和索引添加到您的list

Then iterate through your list , and for each pair of value and index, write the value to the corresponding cell.然后遍历您的list ,并为每对值和索引,将值写入相应的单元格。

Code snippet:代码片段:

newVal.forEach(function(row, index) {
  if (row[0] === origVal[index][0]) {
    list.push([row[0], index]);
  }
});
var firstRow = 2; // Change according to your preferences
var columnIndex = 1; // Change according to your preferences
list.forEach(data => {
  sheet.getRange(data[1] + firstRow, columnIndex).setValue(data[0]);
});

Notes:笔记:

  • I don't know where should the data be written, so I'm using undefined sheet , and also random values for firstRow and columnIndex .我不知道数据应该写在哪里,所以我使用 undefined sheet ,以及firstRowcolumnIndex的随机值。 Please change these according to your preferences.请根据您的喜好更改这些。
  • It would be much more efficient, from a script perspective, to write the whole column to your destination range at once, since that would minimize the number of interactions between the script and the spreadsheet (see Use batch operations ).从脚本的角度来看,一次将整个列写入目标范围会更有效,因为这样可以最大限度地减少脚本和电子表格之间的交互次数(请参阅使用批处理操作)。 Since you want to write only the updated data, though, I provided a script that does this.但是,由于您只想写入更新的数据,因此我提供了一个执行此操作的脚本。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM