【发布时间】:2021-06-09 05:31:44
【问题描述】:
我有两个垂直的一维数组,一个包含现有数据,另一个可能(可能)存在差异。
我希望能够比较两个数组,找出不同之处以及每个不同单元格的索引。
然后,我将使用该信息在现有数据中找到需要更改的适当单元格,并在这些特定单元格中设置新值,而无需复制和粘贴整个数组。
例如:
| Existing Data | New Data |
|---|---|
| John Smith | John Smith |
| 012345 | 012345 |
| 6th grade | 7th grade |
| 555-1234 | 555-1357 |
| Trumpet | Trumpet |
| 5th period | 2nd period |
| Jane Smith | Jane Smith |
| js@email.com | js@email.com |
在这种情况下,代码会看到第 3、4 和 6 行存在差异,将这些新值及其在数组中的位置保存,然后在主数据列表中更新适当的值而不更改任何其他内容。
我尝试了多种方法来比较两个数组并获取有差异的行的索引,这是我所获得的没有错误或“空”结果的情况:
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?,我无法理解您的目标。为了正确理解您的问题,您能否提供您期望的示例输入和输出值?顺便问一下你问题中的表格和你的脚本之间的关系吗? -
嗨,this answer 对您有用吗?如果是这种情况,请考虑接受这个stackoverflow.com/help/someone-answers。
标签: arrays google-apps-script google-sheets indexof