合并间隔的蛮力代码 - Google sheets apps 脚本

Brute force code to merge intervals - Google sheets appscript

我需要 appscript 中的暴力破解代码来将区间合并为一个或多个包含特定 ID 的所有范围的区间。我已经在努力了,但在实际状态下我无法在 appscript 中编程,我希望尽快完成这项任务。

示例:ID 11403

{43896,44463} 
{44245,44245}
{44257,44257}
{44258,44258}
{44258,44258}
{44265,44316}
{44271,44271}
{44277,44279}
{44300,44326}
{44363,44363}
{44363,44363}
{44376,44376}
{44265,44316}
{44271,44271}
{44410,44410}
{44537,44537}
{44540,44553}
{44544,44547}

结果必须是:

{43896,44410}
{44537,44537}
{44540,44553}

Sheet 用途:https://docs.google.com/spreadsheets/d/1UR0xgjCHVxE2Vt0-teSK25f-Kej14Kwfhu5hyhXbDNg/edit?usp=sharing

假设您的 table 看起来像这样:

ID start end
11403 43896 44463
11403 44245 44245
11403 44257 44257
11403 44258 44258
11403 44258 44258
11403 44265 44316
11403 44271 44271
11403 44277 44279
11403 44300 44326
11403 44363 44363
11403 44363 44363
11403 44376 44376
11403 44265 44316
11403 44271 44271
11403 44410 44410
11403 44537 44537
11403 44540 44553
11403 44544 44547
12345 43896 44463
12345 44245 44245
12345 44257 44257

代码如下:

function myFunction() {
  var sh = SpreadsheetApp.getActiveSheet();
  var data = sh.getRange('A2:C').getValues();

  var obj = {}
  while (data.length) {
    var [key, start, end] = data.shift();
    var range = {'start': start, 'end': end};
    try { obj[key].push(range) } catch(e) { obj[key] = [range] };
  }

  var ranges = merge_ranges(obj['11403']); // get the merged ranges for id 11403

  console.log(ranges); // <-------- here are the results (for id 11403)
}


function merge_ranges(obj) {
  var range = obj.shift();
  var end = range.end;
  var ranges = [range];
    
  while (obj.length) {
    var range = obj.shift();
    if (range.start > end) {
      ranges.push(range)
      if (range.end > end) end = range.end;
    }
  }
  return ranges;
}

根据您的数据,它给出了范围数组:

[ { start: 43896, end: 44463 },
  { start: 44537, end: 44537 },
  { start: 44540, end: 44553 } ]

它们看起来与您的结果非常接近。

但是从你的问题中并不清楚你到底想用这些结果做什么。遍历放在 sheet?

上的所有 ID

更新

大概应该是这样的:

function myFunction() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sh = ss.getSheetByName('Página1'); // <----- a name of the source sheet
  var data = sh.getRange('A2:C').getValues();

  // create the object {id1: [range, range, range], id2: [range], ...}
  var obj = {}
  while (data.length) {
    var [id, start, end] = data.shift();
    var range = {'start': start, 'end': end};
    try { obj[id].push(range) } catch(e) { obj[id] = [range] };
  }

  // make the table from the object [id, range.start, range.end]
  var table = [];
  for (var id in obj) {
    var ranges = merge_ranges(obj[id]);
    ranges.forEach(range => table.push([id, range.start, range.end]));
  }
  
  // create or select the sheet 'Output'
  try { var output = ss.getSheetByName('Output'); output.clear(); }
  catch(e) { var output = ss.insertSheet(); output.setName('Output') }

  // add the header to the table and put the table on the sheet
  var header = ['ID', 'Start', 'End'];
  table = [header, ...table];
  var range = output.getRange(1, 1, table.length, table[0].length);
  range.setValues(table);
}

function merge_ranges(obj) {
  var range = obj.shift();
  var end = range.end;
  var ranges = [range];
    
  while (obj.length) {
    var range = obj.shift();
    if (range.start > end) {
      ranges.push(range)
      if (range.end > end) end = range.end;
    }
  }
  return ranges;
}

它将结果作为普通的 3 冒号 table 放在 sheet 'Output' 上。