[英]Paste values from one Google Sheet to another and remove duplicates based on ID column
I have a similar situation to the one described on this question : two worksheets, with input data coming into the Feed sheet using the importxml function and a Data sheet where new rows get copied thanks to a script set to run daily. 我有一个与此问题描述的情况类似的情况:两个工作表,输入数据使用importxml函数输入到Feed表中,而数据表由于设置了每天运行的脚本而可以复制新行。
However, the current script is creating daily duplicates. 但是,当前脚本正在创建每日重复项。 As such, I would like to adapt the answer provided on the question above so that the script checks the IDs on column F and only copies the rows with new IDs.
因此,我想修改上面问题的答案,以便脚本检查F列上的ID,并仅复制具有新ID的行。
How should I update the section below that creates a hash to one that looks for the IDs on column F instead? 我应该如何将下面创建哈希表的部分更新为在F列中查找ID的部分? Also my rows are consistent, so is it correct to assume I can just remove the relevant code lines towards the end?
而且我的行是一致的,因此假设我可以在最后删除相关的代码行是否正确?
The sample Google Sheet is available here . 此处提供了示例Google表格。
function appendUniqueRows() { var ss = SpreadsheetApp.getActive(); var sourceSheet = ss.getSheetByName('Get Data'); var destSheet = ss.getSheetByName('Final Data'); var sourceData = sourceSheet.getDataRange().getValues(); var destData = destSheet.getDataRange().getValues(); // Check whether destination sheet is empty if (destData.length === 1 && "" === destData[0].join('')) { // Empty, so ignore the phantom row destData = []; } // Generate hash for comparisons var destHash = {}; destData.forEach(function(row) { destHash[row.join('')] = true; // could be anything }); // Concatentate source rows to dest rows if they satisfy a uniqueness filter var mergedData = destData.concat(sourceData.filter(function (row) { var hashedRow = row.join(''); if (!destHash.hasOwnProperty(hashedRow)) { // This row is unique destHash[hashedRow] = true; // Add to hash for future comparisons return true; // filter -> true } return false; // not unique, filter -> false })); // Check whether two data sets were the same width var sourceWidth = (sourceData.length > 0) ? sourceData[0].length : 0; var destWidth = (destData.length > 0) ? destData[0].length : 0; if (sourceWidth !== destWidth) { // Pad out all columns for the new row var mergedWidth = Math.max(sourceWidth,destWidth); for (var row=0; row<mergedData.length; row++) { for (var col=mergedData[row].length; col<mergedWidth; col++) mergedData[row].push(''); } } // Write merged data to destination sheet destSheet.getRange(1, 1, mergedData.length, mergedData[0].length) .setValues(mergedData); }
I'm a novice in this world of Google Apps scripts, so do please let me know if I'm missing any crucial information. 我是Google Apps脚本领域的新手,所以如果我缺少任何重要信息,请告诉我。 Thanks in advance for the help.
先谢谢您的帮助。
If my understanding for your question is correct, how about this modification? 如果我对您的问题的理解是正确的,那么此修改如何? In this modification, I modified the script in your shared spreadsheet.
在此修改中,我修改了共享电子表格中的脚本。
The script which reflected above flow is as follows. 以上流程反映的脚本如下。
This is your script in the shared spreadsheet. 这是共享电子表格中的脚本。 Please modify this script to below one.
请将该脚本修改为以下脚本。
function Copy() { var sss = SpreadsheetApp.openById('#####'); // this is your Spreadsheet key var ss = sss.getSheetByName('Feed'); // this is the name of your source Sheet tab var range = ss.getRange('A3:H52'); //assign the range you want to copy var data = range.getValues(); var tss = SpreadsheetApp.openById('#####'); //replace with destination ID var ts = tss.getSheetByName('Data'); //replace with destination Sheet tab name ts.getRange(ts.getLastRow()+1, 1,50,8).setValues(data);// 49 value refers to number of rows, 8 to columns }
To:
function Copy() { var sss = SpreadsheetApp.openById('#####'); // this is your Spreadsheet key var ss = sss.getSheetByName('Feed'); // this is the name of your source Sheet tab var range = ss.getRange('A3:H52'); //assign the range you want to copy var data = range.getValues(); var tss = SpreadsheetApp.openById('#####'); //replace with destination ID var ts = tss.getSheetByName('Data'); //replace with destination Sheet tab name // Below script was added. var values = ts.getRange("F3:F").getValues().filter(String); var copiedValues = data.filter(function(e) {return !values.some(function(f){return f[0] == e[5]}) && e.filter(String).length > 0}); ts.getRange(ts.getLastRow() + 1, 1, copiedValues.length, copiedValues[0].length).setValues(copiedValues); }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.