如何从A1Notation获取行和列值

Mog*_*dad 3 javascript google-sheets google-apps-script

有时候为Google Spreadsheets编写脚本很困难,因为采用行编号和列编号的Google Spreadsheet方法使用基于1的索引,而Javascript数组使用基于0的索引.

截图

在这个例子中,单元格A2有一个row == 2column == 1.SpreadsheetApp方法反转A1Notation中的列和行,因此这两个范围是等效的:

var range1 = sheet.getRange("A2");
var range2 = sheet.getRange(2, 1);
Run Code Online (Sandbox Code Playgroud)

一旦我将工作表的内容读入数组,事情就会再次发生变化.

var ss = SpreadsheetApp.getActive();
var sheet = ss.getActiveSheet();
var data = sheet.getDataRange().getValues();
Run Code Online (Sandbox Code Playgroud)

之后,A2我的电子表格中的单元格中的值为data[1][0].行和列的顺序与SpreadsheetApp API的顺序相同,但每个都少了1个.

截图2

这里(示例)的许多问题的答案归结为这些不同形式的索引不匹配.充满代码row+1col-1语句的代码很难调试.

最后:如果我知道A1Notation中单元格的引用,比如说AZ342,如何找到data与完整数据范围中获得的2D数组中该单元格对应的正确索引值?

Mog*_*dad 7

显然,你可以非常小心地跟踪你使用任何一种索引类型的地方,你会没事的.

但是做这样的事情会更容易:

var importantCell = "AZ342";
var cellIndexConverted = cellA1ToIndex( importantCell );
var data[cellIndexConverted.row][cellIndexConverted.col] = "Some new value";
Run Code Online (Sandbox Code Playgroud)

ConvertA1.gs

这里有三个辅助函数来简化从A1Notation的转换.

这些辅助函数也可作为要点.

/**
 * Convert a cell reference from A1Notation to 0-based indices (for arrays)
 * or 1-based indices (for Spreadsheet Service methods).
 *
 * @param {String}    cellA1   Cell reference to be converted.
 * @param {Number}    index    (optional, default 0) Indicate 0 or 1 indexing
 *
 * @return {object}            {row,col}, both 0-based array indices.
 *
 * @throws                     Error if invalid parameter
 */
function cellA1ToIndex( cellA1, index ) {
  // Ensure index is (default) 0 or 1, no other values accepted.
  index = index || 0;
  index = (index == 0) ? 0 : 1;

  // Use regex match to find column & row references.
  // Must start with letters, end with numbers.
  // This regex still allows induhviduals to provide illegal strings like "AB.#%123"
  var match = cellA1.match(/(^[A-Z]+)|([0-9]+$)/gm);

  if (match.length != 2) throw new Error( "Invalid cell reference" );

  var colA1 = match[0];
  var rowA1 = match[1];

  return { row: rowA1ToIndex( rowA1, index ),
           col: colA1ToIndex( colA1, index ) };
}

/**
 * Return a 0-based array index corresponding to a spreadsheet column
 * label, as in A1 notation.
 *
 * @param {String}    colA1    Column label to be converted.
 *
 * @return {Number}            0-based array index.
 * @param {Number}    index    (optional, default 0) Indicate 0 or 1 indexing
 *
 * @throws                     Error if invalid parameter
 */
function colA1ToIndex( colA1, index ) {
  if (typeof colA1 !== 'string' || colA1.length > 2) 
    throw new Error( "Expected column label." );

  // Ensure index is (default) 0 or 1, no other values accepted.
  index = index || 0;
  index = (index == 0) ? 0 : 1;

  var A = "A".charCodeAt(0);

  var number = colA1.charCodeAt(colA1.length-1) - A;
  if (colA1.length == 2) {
    number += 26 * (colA1.charCodeAt(0) - A + 1);
  }
  return number + index;
}


/**
 * Return a 0-based array index corresponding to a spreadsheet row
 * number, as in A1 notation. Almost pointless, really, but maintains
 * symmetry with colA1ToIndex().
 *
 * @param {Number}    rowA1    Row number to be converted.
 * @param {Number}    index    (optional, default 0) Indicate 0 or 1 indexing
 *
 * @return {Number}            0-based array index.
 */
function rowA1ToIndex( rowA1, index ) {
  // Ensure index is (default) 0 or 1, no other values accepted.
  index = index || 0;
  index = (index == 0) ? 0 : 1;

  return rowA1 - 1 + index;
}
Run Code Online (Sandbox Code Playgroud)