Google表格应用脚本-如何仅将setValues仅设置为唯一值

时间:2019-09-07 07:50:10

标签: google-apps-script

在此电子表格中:https://docs.google.com/spreadsheets/d/1givNbMvgzD8lbk6NAcwjkpp4-A_D8MetltHjEpinOAI/edit#gid=0

我只想将唯一的<div class="col-md-12 b-top" style="padding:50px 30px;"> <div class="col-md-6 b-shadow" style=" padding:20px"> <img src="https://image.shutterstock.com/z/stock-vector-question-mark-in-a-speech-bubble-icon-720160648.jpg" width="50px" /> <div style="color:#000;"> <h3 style="font-weight:600;">Learning Center</h3> <p>Use the Learning Center&nbsp;to find an answer on your own.&nbsp;</p> </div> </div> <div class="col-md-6 b-shadow" style="padding:20px;"> <img src="https://image.shutterstock.com/z/stock-vector-question-mark-in-a-speech-bubble-icon-720160648.jpg" width="50px" /> <div style="color:#000;"> <h3 style="font-weight:600;">Learning Center</h3> <p>Use the Learning Center&nbsp;to find an answer on your own.&nbsp;</p> </div> </div> </div>Links合并到Category表中。

现在,使用我的脚本,它只能合并所有现有数据:

Combined

但是,我只想合并function combine() { var ss = SpreadsheetApp.getActive(); var allsheets = ss.getSheets(); var sourceID = '1givNbMvgzD8lbk6NAcwjkpp4-A_D8MetltHjEpinOAI'; var targetID = '1givNbMvgzD8lbk6NAcwjkpp4-A_D8MetltHjEpinOAI'; var sheetExclude = ["Combined"]; var sheetExcludeIndex = new Array(sheetExclude.length); for (var s in allsheets) { var sheet = allsheets[s]; for (var e in sheetExclude) { if (String(sheet.getName() == sheetExclude[e])) { sheetExcludeIndex[e] = sheet.getIndex; } } } allsheets.splice(sheetExcludeIndex, sheetExclude.length); for (var s in allsheets) { var sheet = allsheets[s]; updateSourceToTarget(sourceID, sheet.getName(), targetID, 'Combined'); } } function updateSourceToTarget(sourceID, sourceName, targetID, targetname) { Logger.log(sourceID + ' ' + sourceName + ' ' +targetname); var source = SpreadsheetApp.openById(sourceID).getSheetByName(sourceName); var destination = SpreadsheetApp.openById(targetID).getSheetByName(targetname); var sourcelastRow = source.getLastRow(); var sourcelastCol = source.getLastColumn(); var destinationlastRow = destination.getLastRow(); var destinationlastCol = destination.getLastColumn(); var sourcedata = source.getRange(2, 9, sourcelastRow, 10).getValues(); destination.getRange(destinationlastRow + 1, 2, sourcelastRow, sourcelastCol).setValues(sourcedata); } Sheet2中的唯一链接:

红色为唯一数据

Sheet2: enter image description here

Sheet3: enter image description here

如何有效地将唯一值从Sheet3Combined仅添加到Sheet2

1 个答案:

答案 0 :(得分:2)

  • 您要将删除重复链接的值放入目标工作表(在本例中为Combined工作表。)。
  • 从目标工作表和源工作表中检查重复的链接。在这种情况下,目标工作表和源工作表分别为CombinedSheet2Sheet3
    • 在示例电子表格中,您要将以下行放入目标表。
      • https://thehill.com/policy/national-security/department-of-homeland-security/460158-new-hampshire-border-patrol BorderSecurity
      • https://abcnews.go.com/International/climate-change-frontier-worlds-northernmost-town/story?id=65381362 ClimateChange
  • 您想通过修改Google Apps脚本来实现这一目标。

如果我的理解是正确的,那么该修改如何?请认为这只是几个答案之一。

修改后的脚本:

请按如下所示修改脚本。在此修改中,未使用updateSourceToTarget()的功能。

发件人:

for (var s in allsheets) {
    var sheet = allsheets[s];
    updateSourceToTarget(sourceID, sheet.getName(), targetID, 'Combined');
}

收件人:

// Retrieve values from the target sheet.
var targetSheet = ss.getSheetByName(sheetExclude[0]);
var targetValues = targetSheet.getRange("B2:C" + targetSheet.getLastRow()).getValues();

// Retrieve values from all source sheets. <--- Modified
var sourceValues = allsheets.reduce(function(ar, sheet) {
  var v = sheet.getRange(2, 9, sheet.getLastRow() - 1, 10).getValues().filter(function(r) {return r[0] && r[1]});
  if (v.length > 0) {
    v = v.filter(function(e) {return !ar.some(function(f) {return e[0] === f[0]})});
    Array.prototype.push.apply(ar, v);
  }
  return ar;
}, []);

// Remove the duplication values between the target sheet and all source sheets.
var dstValues = sourceValues.filter(function(e) {return !targetValues.some(function(f) {return e[0] === f[0]})});

// Add the result values to the target sheet.
if (dstValues.length > 0) {
  var destination = SpreadsheetApp.openById(targetID).getSheetByName(sheetExclude[0]);
  destination.getRange(destination.getLastRow() + 1, 2, dstValues.length, dstValues[0].length).setValues(dstValues);
}

此修改后的脚本的流程如下。

  1. 从目标表中检索值。
  2. 从所有原始工作表中检索值。
  3. 删除目标工作表和所有源工作表之间的重复值。
  4. 将结果值添加到目标表中。

注意:

  • 将共享的电子表格用作目标(Combined和源电子表格(Sheet2Sheet3)时,会将以下行添加到目标表格。
    • https://thehill.com/policy/national-security/department-of-homeland-security/460158-new-hampshire-border-patrol BorderSecurity
    • https://abcnews.go.com/International/climate-change-frontier-worlds-northernmost-town/story?id=65381362 ClimateChange

参考文献:

如果我误解了您的问题,而这不是您想要的方向,我深表歉意。

已添加:

在此附加脚本中,哈希表用于这种情况,如TheMaster's comment所述。例如,还可以在this thread看到示例。根据您的情况,首先,从包括Combined工作表在内的所有工作表中检索所有值,并创建哈希表。这样,删除了重复的值。然后,将转换为数组的值放入电子表格。

示例脚本:

请按如下所示修改脚本。

从:
allsheets.splice(sheetExcludeIndex, sheetExclude.length);

for (var s in allsheets) {
    var sheet = allsheets[s];
    updateSourceToTarget(sourceID, sheet.getName(), targetID, 'Combined');
}
至:
// allsheets.splice(sheetExcludeIndex, sheetExclude.length); // In this script, this line is not used.

// Retrieve values from the target sheet.
var targetSheet = ss.getSheetByName(sheetExclude[0]);
var targetValues = targetSheet.getRange("B2:C" + targetSheet.getLastRow()).getValues();

// Retrieve values from all source sheets.
// Remove the duplication values between the target sheet and all source sheets.
var sourceValues = allsheets.reduce(function(obj, sheet) {
  var v = sheet.getRange(2, 9, sheet.getLastRow() - 1, 10).getValues().filter(function(r) {return r[0] && r[1]});
  if (v.length > 0) v.forEach(function(e) {if (!(e[0] in obj)) obj[e[0]] = e[1]});
  return obj;
}, {});
var dstValues = Object.keys(sourceValues).map(function(e) {return [e, sourceValues[e]]});

// Add the result values to the target sheet.
if (dstValues.length > 0) {
  var destination = SpreadsheetApp.openById(targetID).getSheetByName(sheetExclude[0]);
  destination.getRange(2, 2, destination.getLastRow(), 2).clearContent();
  destination.getRange(2, 2, dstValues.length, dstValues[0].length).setValues(dstValues);
}
  • 建议的2个示例脚本可用于您的情况。因此,请选择其中之一。