简体   繁体   English

将值从一个Google表格粘贴到另一个,然后根据ID列删除重复项

[英]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. 先谢谢您的帮助。

  • You want to copy the values from "Feed" sheet to "Data" sheet. 您要将值从“进纸”表复制到“数据”表。
  • When the values are copied, you want to copy only new values which are not included in "Data" sheet. 复制值时,您只想复制“数据”表中未包括的新值。
  • You want to choose the new values using the values of column "F". 您想使用“ F”列的值选择新值。

If my understanding for your question is correct, how about this modification? 如果我对您的问题的理解是正确的,那么此修改如何? In this modification, I modified the script in your shared spreadsheet. 在此修改中,我修改了共享电子表格中的脚本。

Modification points: 修改要点:

  • In your script, all values of "Feed" sheet are copied to "Data" sheet. 在脚本中,“进纸”表的所有值都将复制到“数据”表。 So in order to choose only new values, I used the following flow. 因此,为了仅选择新值,我使用了以下流程。
    1. Retrieve the values from column "F". 从“ F”列中检索值。 This is used for choosing the new values. 这用于选择新值。
    2. Retrieve the new values using the values from column "F". 使用“ F”列中的值检索新值。
    3. Put the new values to "Data" sheet. 将新值放入“数据”表。

The script which reflected above flow is as follows. 以上流程反映的脚本如下。

Modified script: 修改后的脚本:

From: 从:

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.

相关问题 将值从一张纸粘贴到另一张纸并删除重复的纸 - Paste values from one sheet to another and remove duplicates 根据条件将单元格值从一个Google工作表复制到另一个 - Copy cell values from one google sheet to another based on condition 将背景颜色从一列复制/粘贴到具有匹配值的列中的另一张工作表 - Copy / paste background color from one column to another sheet in a column with matched values 如何使用 Google Sheets Macros 从一张工作表复制值并将它们粘贴到另一张工作表中? - How to copy values from one sheet and paste them into another using Google Sheets Macros? 如何将值从一张纸粘贴到另一张纸到特定列的最后一行 - How to paste values from one sheet to another to last row of specific column 如何使用 Google 脚本从 1 个 Google 工作表中复制特定值并粘贴到另一个工作表? - How can I copy specific values from 1 Google Sheet and paste to another Sheet using Google Script? 根据JavaScript中的一列从多列中删除重复项 - Remove duplicates from multicolumns based on one column in Javascript Google电子表格将值从一行复制到另一张工作表 - Google spreadsheet copy values from one row to another sheet Google Sheet App Script:将 1 个值从一张纸匹配到另一张纸,然后如果条件满足设置背景 - Google Sheet App Script: Match 1 values from one sheet to another sheet and then if condition met set background 根据 Google Sheet 脚本第一列合并单元格中的日期从工作表锁定异常中删除行 - Remove rows from sheet lock exception based on date in merged cell in first column in Google Sheet script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM