简体   繁体   English

谷歌表格脚本:从当前单元格获取多列/行命名范围

[英]Google Sheets Script: Get Multi-Col/Row Named Range from Current Cell

From other examples, I have a function that returns the name of a Named Range for the current cell - but only if the range consists of a single cell.从其他示例中,我有一个 function 返回当前单元格的命名范围的名称 - 但前提是该范围由单个单元格组成。

I started trying to parse the starting and ending Col and Row of the range, but ran into problems as soon as cols went to more than one character, eg "AA".我开始尝试解析范围的开始和结束 Col 和 Row,但是一旦 cols 出现多个字符,例如“AA”,就会遇到问题。

Then I thought that if I could get the cell's "R1C1" notation, I could use getCell() to test if it exists in the range.然后我想如果我能得到单元格的“R1C1”符号,我可以使用 getCell() 来测试它是否存在于范围内。 However, I can't find an equivalent function of getA1Notation() to get the R1C1 of the current cell.但是,我找不到 getA1Notation() 的等效 function 来获取当前单元格的 R1C1。 How is it obtained pls?请问它是如何获得的?

function c_GetCellNamedRange() {
  const ui = SpreadsheetApp.getUi();
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  const cell = sheet.getCurrentCell().getA1Notation();
  ui.alert("Current Cell: " + cell);

  //get all the name ranges of the spreadsheetsheet
  var namedRanges = SpreadsheetApp.getActiveSpreadsheet().getNamedRanges();
  // loop through all the names range of the sheet
  for(i=0;i<namedRanges.length;i++){
  
/*  
    // this only works if range is a single cell! How to loop through?
    if(namedRanges[i].getRange().getA1Notation() == A1Notation) {
      rangeName = namedRanges[i].getName();
      break;
    }
*/
    // get the current cell R1C1 notation, then use getCell() to test if
    // it exists in the named range?
    //var rv = namedRanges[i].getCell(cell.getRow(), cell.getColumn())
    var rv = namedRanges[i].getCell(cell.getRowIndex(), cell.getColumn())
    if (!rv ) {
      rangeName = namedRanges[i].getName();
      break;
    }

  }

  if ( !rangeName ) rangeName = "none"
  ui.alert("Current Cell:   " + cell + " \r\n\r\n Named Range:   " + rangeName);
}

You can get the row index and the column index of a cell using getRow() and getColumn() .您可以使用getRow()getColumn()获取单元格的行索引和列索引。 Then you can use that to write its R1C1 notation:然后你可以用它来写它的 R1C1 符号:

const r1c1Notation = `R${sheet.getCurrentCell().getRowIndex()}C${sheet.getCurrentCell().getColumn()}`;

If I understood you correctly you want to loop through each cell of a named range to compare the A1 notation of the cells with a variable如果我理解正确,您想遍历命名范围的每个单元格,以将单元格的 A1 表示法与变量进行比较

  • To do so, you can use the method range.getCell() withinh a nested loop - since the range is a 2-D object.为此,您可以在嵌套循环中使用方法range.getCell() - 因为范围是二维 object。
  • One of the ways to establish the height and width of the range is to use the methods getRow() , getLastRow() , getColumn() and getLastColumn() .确定范围的高度和宽度的方法之一是使用方法getRow()getLastRow()getColumn()getLastColumn()

Sample:样本:

function myFunction() {
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var namedRanges = SpreadsheetApp.getActiveSpreadsheet().getNamedRanges();
  var A1Notation = "B5";
  for(var i = 0;i < namedRanges.length; i++){
    Logger.log(namedRanges[i].getRange().getA1Notation());
    var range = namedRanges[i].getRange();    
    var rangeWidth = range.getWidth();
    var rangeHeight = range.getHeight();
    for (var j = 1; j <= rangeWidth; j++){ 
      for (var k = 1; k <= rangeHeight; k++){ 
        var A1 = range.getCell(j, k).getA1Notation();
        if (A1 == A1Notation){
          rangeName = namedRanges[i].getName();
          break;
        }      
      }
    }
  }
}

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

相关问题 谷歌表格脚本 - 从活动单元格中获取命名范围 - Google Sheets scripts - get named range from active cell 谷歌表格脚本:为命名范围内的每个单元格获取 A1Notation - Google Sheets Script: Get A1Notation For Each Cell In Named Range Google Spreadsheets函数通过行和列名称从范围中获取单元格 - Google Spreadsheets function to get a cell from a range by row and col name Google 表格脚本:获取命名范围中的第一行号 - Google Sheets Script: Get First Row Number in Named Range 谷歌表格脚本:获取表格命名范围已打开 - Google Sheets Script: Get Sheet a Named Range is On Google Sheets API 脚本:尝试通过调用命名范围来获取特定列的最后一行 - Google Sheets API script: Trying to get the last row of a specific column by calling a named range Google Sheets 脚本 - 对于范围 1 中的每个使用过的单元格,如果单元格值存在于范围 2 中,则获取范围 2 匹配的行号 - Google Sheets script - For each used cell in range 1, if cell value exists in range 2, get range 2 match's row number 从命名范围Google表格脚本中选择范围 - Selecting a range from within a named range google sheets script Google Sheets Apps 脚本,从命名范围返回随机项目 - Google Sheets Apps Script, return random item from named range Google脚本-从单个单元格获取范围 - Google Script - Get Range from single cell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM