簡體   English   中英

在兩張不同的紙上比較 arrays 並只取唯一值

[英]Compare arrays on two different sheets and take only unique values

我有一個腳本,它創建一個定時觸發器,將數據從源工作表移動到目標工作表。 我想添加的是僅從源表中獲取目標表中不存在的唯一/新值並將它們添加到目標表的能力。

我目前擁有的腳本將源工作表中的所有非空值傳遞到一個數組中,然后將其附加或寫入目標數據。

我嘗試讀取兩個工作表的值並在兩個集合中使用 for 循環並將不相等的值推送到一個新數組中,但最終只是復制了工作表整個長度的所有值。 我也嘗試過連接兩個 arrays 然后循環遍歷它以獲得唯一值,但最終也給了我空白。

function appendUniqueRows() {
  const ss1=SpreadsheetApp.openById("ss1id");//source
  const sh1=ss1.getSheetByName('ss1shname')
  const rg1=sh1.getDataRange();//this range should be data only
  const v1=rg1.getValues();
  const ss2=SpreadsheetApp.openById("ss2id");//destination
  const sh2=ss2.getSheetByName("ss2shname");
  const rg2=sh2.getDataRange();//this range should be  dataonly
  const v2=rg2.getValues();
  const d=v2.map(r=>{return r.join("")});//current rows in destination
  v1.forEach(r=>{if(!d.includes(r.join(""))){d.push(r.join(""));sh2.appendRow(r);}});//append unique rows
}

將 setValues() 與 output 數組一起使用

function appendUniqueRows() {
  const ss1=SpreadsheetApp.openById("ss1id");//source
  const sh1=ss1.getSheetByName('ss1shname')
  const rg1=sh1.getDataRange();//this range should be data only
  const v1=rg1.getValues();
  const ss2=SpreadsheetApp.openById("ss2id");//destination
  const sh2=ss2.getSheetByName("ss2shname");
  const rg2=sh2.getDataRange();//this range should be  dataonly
  const v2=rg2.getValues();
  const d=v2.map(r=>{return r.join("")});//current rows in destination
  let oA=[];
  v1.forEach(r=>{if(!d.includes(r.join(""))){d.push(r.join(""));oA.push(r)}});
  sh2.clearContents();
  sh2.getRange(1,1,oA.length,oA[0].length).setValues(oA);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM