【问题标题】:Using GSheets Script to increment value of duplicate by 1使用 GSheets 脚本将重复值增加 1
【发布时间】:2020-11-27 12:26:55
【问题描述】:

我想检查一行是否有重复项,如果匹配则将它们加 1。

我想以这种方式操作的数据是 LAT LONG 坐标。 它们源自聚合数据采集,用户只能插入国家和城市。通过附加组件,这些将获得 GEO 编码。 问题是,我需要稍微更改重复条目的值,以便在地图上显示它们。最简单的方法(我认为)是如果已经存在具有相同值的条目,则将 LAT 或 LONG 坐标增加 1。

Data sample & results of script

知道如何通过脚本做到这一点吗?

我的代码,原本打算删除重复行:

function overwriteDuplicates() {
  var ss=SpreadsheetApp.getActive();
  var sh=ss.getSheetByName("Formularantworten 2");
  var vA=sh.getDataRange().getValues();
  var hA=vA[0];
  var hObj={};
  hA.forEach(function(e,i){hObj[e]=i;});//header title to index
  var uA=[];
  var d=0;
  for(var i=0;i<vA.length;i++) {
    if(uA.indexOf(vA[i][hObj['Latitude']])==-1) {
      uA.push(vA[i][hObj['Latitude']]);
    }else{
     function increment() {
 SpreadsheetApp.getActiveSheet().getRange('K').setValue(SpreadsheetApp.getActiveSheet().getRange('K').getValue() + 1);
}
    }}}

【问题讨论】:

  • 如果您共享样本表、显示样本数据以及您希望看到的结果 - 手动输入,这总是很有帮助的。请参阅此处了解如何分享:webapps.stackexchange.com/questions/138382/…。其次,增加重复项到底是什么意思?一行中的重复值?看一个例子可能有助于我们理解。
  • 在您提供的屏幕截图中(1),不仅有增量,还有减量,835 转换为834835836,所以它不仅是增量吗? (2)经度没有改变,它也应该改变吗?,(3)在同一行,不同列中创建重复项(总是B:E将在H:K中重复?)。你能澄清这一切吗?
  • 1) 仅将835 增加到836 等就足够了。我只需要生成一对新的 LAT LONG 坐标。屏幕截图在那里有点误导。 2) LONG 坐标不必为此进行必要的更改。只需更改 LAT 或 LONG 中的任何一个,就会生成一个新的坐标对。 3) H:K 显示数据操作的目标。我只想更改B:E 中的重复项,以便最终表看起来像H:K

标签: google-sheets duplicates increment


【解决方案1】:

每当坐标重复(发现纬度和经度)时,您都想修改纬度。

在这种情况下,您可以执行以下操作:

  • 获取工作表中的所有值,并根据标题检索LatitudeLongitude 所在的索引。
  • 遍历所有行(不包括标题),并为每一行检查前一行是否包含这些坐标。
  • 如果是这种情况 (isDuplicate),请增加纬度中的最后一个数字(在最后一个 . 之后),或者如果它接近 999 则减少它(我假设总是有 3 位数字)。李>
  • 修改后将新纬度存储在数组中。
  • 使用setValues 将修改后的纬度写入工作表。

以下是执行此操作的可能代码 sn-p(有关详细信息,请参阅内联 cmets)。

代码sn-p:

function overwriteDuplicates() {
  const ss = SpreadsheetApp.getActive();
  const sheet = ss.getSheetByName("Formularantworten 2");
  const values = sheet.getDataRange().getValues();
  const headers = values.shift(); // Retrieve first row (headers)
  const latColumn = headers.indexOf("Latitude"); // Latitude column index
  const longColumn = headers.indexOf("Longitude"); // Longitude column index
  let outputLocations = [];
  for (let i = 0; i < values.length; i++) { // Loop through rows
    const newLocation = { // Store object with current location
      "latitude": values[i][latColumn],
      "longitude": values[i][longColumn]
    }
    let isDuplicate;
    do { // Check if location is duplicate, and increment if necessary
      isDuplicate = outputLocations.some(location => { 
        const sameLatitude = location["latitude"] === newLocation["latitude"];
        const sameLongitude = location["longitude"] === newLocation["longitude"];
        return sameLatitude && sameLongitude;
      });
      if (isDuplicate) newLocation["latitude"] = modifyCoord(newLocation["latitude"]);
    } while (isDuplicate);
    outputLocations.push(newLocation); // Store new location in array
  }
  let outputLatitudes = outputLocations.map(location => [location["latitude"]]); // Retrieve latitudes from array
  sheet.getRange(2, latColumn+1, sheet.getLastRow()-1).setValues(outputLatitudes); // Write updated latitudes to sheet
}
 
function modifyCoord(coordinates) {
  let coordArray = coordinates.split(".");
  let lastCoord = coordArray.pop();
  if (lastCoord > 990) lastCoord--;
  else lastCoord++;
  coordArray.push(lastCoord);
  return coordArray.join(".");
}

输出:

【讨论】:

  • 非常感谢lamblichus!还是新的,所以我不能投票,但投票在我心里!
  • @HERMINE_e.V.别客气。你不能投票,但你可以接受答案;)见stackoverflow.com/help/someone-answers
猜你喜欢
  • 1970-01-01
  • 2012-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-01
相关资源
最近更新 更多