[英]Google Apps Script to find and update rows in target sheet by unique ID and add unique rows if unique ID is not in the target sheet
[英]Compare data in source sheet with target sheet and copy missing rows to target sheet with Google Apps Script
源表有 8 列,目標表有 9 列(注意前 8 列相同,第 9 列用於設置 url 鏈接,一旦它被郵寄出去)。 我不希望對目標表進行排序。 url 鏈接必須位於其所屬的相應行上。
這是我目前正在使用的代碼。 它確實提取並比較了數據,但它會在每次運行的開始和之后不斷添加一個空白行。 我不明白為什么會這樣?
function addNewStudents() {
let ss = SpreadsheetApp.getActiveSpreadsheet()
let source = ss.getSheetByName('Accepted Students')
let sourceValues = source.getRange(2, 1, source.getLastRow(), 8).getValues().filter(String)
//sourceValues.shift()
//Logger.log(sourceValues)
let targetSheet = ss.getSheetByName('Confirmation Letters')
let targetValues = targetSheet.getRange(2, 1, targetSheet.getLastRow(), 8).getValues().filter(String)
//console.log(targetValues)
let diff = targetValues.showDif(sourceValues)
targetValues = (diff.length && diff) ? targetValues.concat(diff) : targetValues
// console.log(targetValues)
if (targetValues === '') {
var ui = SpreadsheetApp.getUi()
ui.alert("No new students")
} else {
targetSheet.getRange(2, 1, targetValues.length, targetValues[0].length).setValues(targetValues)
}
//console.log(targetValues.length)
}
Array.prototype.showDif = function (array) {
let that = this;
return array.filter(function (r) {
return !that.some(function (x) {
return r.join() === x.join()
})
})
}
您會在目標工作表的底部找到缺失的行
function addNewStudents() {
const ss = SpreadsheetApp.getActive()
const ssh = ss.getSheetByName('Accepted Students')
const svs = ssh.getRange(2, 1, ssh.getLastRow(), 8).getValues();
const tsh = ss.getSheetByName('Confirmation Letters')
const tvs = tsh.getRange(2, 1, tsh.getLastRow(), 8).getValues();
let tA = tvs.map(r => r.join(""));
svs.forEach((r,i) => {
if(!~tA.indexOf(r.join(""))) {
tvs.push(r)
}
});
tsh.getRange(2,1,tsh.getLastRow() - 1, tsh.getLastColumn()).clearContent();
tsh.getRange(2,1,tvs.length,tvs[0].length).setValues(tvs);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.