简体   繁体   English

获取选定范围内的值和行号,单元格之间有间隙?

[英]Get a value and row number in selected range with gaps between cells?

所选范围示例

I need to get values and row number in Google sheet script out of sellected range.我需要在 Google 工作表脚本中获取超出选定范围的值和行号。

Original problem was that I was trying to get values and row numbers of sellected range using this script:最初的问题是我试图使用此脚本获取选定范围的值和行号:

sheet = ss.getActiveSheet(),
  sheetvalues = sheet.getActiveRange().getValues();
  for (i=0; i<sheetvalues.length; i++) {
  mr = sheet.getActiveRange().getRow()+i;
  }

But I found out that if you use filer and select a range then all the hidden cells will also be part of the activerange, but I need only those that are inside of a filtered range.但我发现,如果您使用 filer 和 select 范围,那么所有隐藏单元格也将成为活动范围的一部分,但我只需要那些在过滤范围内的单元格。 I decided that the best way is to select cells separately one by one.By I can't get a value and row index cause it counts as active range only the last selected cell.我决定最好的方法是 select 单元格一个一个地分开。通过我无法获得值和行索引,因为它仅将最后一个选定的单元格计为活动范围。

I believe your goal as follows.我相信你的目标如下。

  • Under the selected rows for the sheet, you want to retrieve the row numbers of no selected rows using Google Apps Script.在工作表的选定行下,您想使用 Google Apps 脚本检索未选定行的行号。

In this case, I think that the method of getSelection can be used.在这种情况下,我认为可以使用getSelection的方法。 And, in order to confirm the rows, getActiveRangeList is used.并且,为了确认行,使用getActiveRangeList When this is reflected to a sample script, it becomes as follows.当这反映到示例脚本时,它变成如下。

Sample script:示例脚本:

When you use this script, at first, please select the cells like your sample image and run the script.当您使用此脚本时,首先请 select 像您的示例图像一样的单元格并运行该脚本。

function myFunction() {
  // 1. Retrieve the selected ranges.
  const selection = SpreadsheetApp.getSelection();

  // 2. Retrieve the selected row numbers.
  const selectedRows = selection.getActiveRangeList().getRanges().flatMap(r => {
    const row = r.getRow();
    const temp = [];
    for (let i = 0; i < r.getNumRows(); i++) {
      temp.push(row + i);
    }
    return temp;
  }).sort((a, b) => a - b);

  // 3. From data range, retrieve the row numbers except for the selected row numbers.
  const sheet = selection.getActiveSheet();
  const range = sheet.getDataRange();
  const rowNumbers = [];
  for (let r = 1; r <= range.getNumRows(); r++) {
    if (!selectedRows.includes(r)) rowNumbers.push(r);
  }
  console.log(rowNumbers); // Here, you can see the retrieved row numbers at the log.

  // 4. Retrieve the row values of the retrieved row numbers.
  // If you want to retrieve the row values of "rowNumbers", you can also the following script.
  const allValues = range.getValues();
  const values = rowNumbers.map(n => allValues[n]);
  console.log(values); // Here, you can see the values of row numbers at the log.
}
  • As the important point for using the selected range, for example, when the cells of row 3, row 1, row 2 are selected in order, the range list returns the order of cells.作为使用所选范围的重点,例如,当依次选择第 3 行、第 1 行、第 2 行的单元格时,范围列表返回单元格的顺序。 So in this sample script, the retrieved row numbers are sorted.所以在这个示例脚本中,检索到的行号是排序的。

  • When above script is run, for example, when the data range is "A1:C5" and when the cells "A1", "A3" and "A5" are selected, the row numbers of 2, 4 are retrieved.例如,在运行上面的脚本时,当数据范围为“ A1:C5”时,并且选择单元格“ A1”,“ A3”和“ A5”时2, 4的行。

References:参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM