> For the complete documentation index, see [llms.txt](https://lochiwei.gitbook.io/web/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://lochiwei.gitbook.io/web/appendix/gas/classes/range/a1-notation/a1notationtorowcolumn.md).

# A1NotationToRowColumn()

[GAS](/web/appendix/gas.md) ⟩ [Classes](/web/appendix/gas/classes.md) ⟩ [Range](/web/appendix/gas/classes/range.md) ⟩ [A1 notation](/web/appendix/gas/classes/range/a1-notation.md) ⟩ A1NotationToRowColumn()

{% tabs %}
{% tab title="💾 程式" %}

* [replit](https://replit.com/@pegasusroe/A1NotationToRowColumn#index.js)&#x20;

{% hint style="info" %}
equivalent to [=ROW()](https://support.google.com/docs/answer/3093316?hl=en\&ref_topic=3105472), [=COLUMN()](https://support.google.com/docs/answer/3093373?hl=en\&ref_topic=3105472) in Google Sheets.
{% endhint %}

```javascript
/**
 * A1 notation to [row, col]
 *
 * @param {string} a1notation - cell address in "A1 notation"
 * @returns {number[]} [row, col] (1-based)
 *
 * @example
 *
 *   A1NotationToRowColumn("A2")   // [ 2,  1 ]
 *   A1NotationToRowColumn("AA3")  // { 3, 27 ]
 */
function A1NotationToRowColumn(a1notation) {

    // character index (start from 1)
    // ------------------------------
    // A -> 1, ..., Z -> 26
    function CharIndex(char){
        return char.charCodeAt() - 'A'.charCodeAt() + 1
    }

    // capture groups                                            ╭──1───╮╭──2───╮
    const [_, columnName, row] = a1notation.toUpperCase().match(/([A-Z]+)([0-9]+)/);
    const numLetters = 26;    // A - Z
    
    // 'ABC' -> [A,B,C] -> ((0*26 + A)*26 + B)*26 + C = A*26^2 + B*26^1 + C
    let col = columnName
        .split('')
        .reduce((sum, char) => sum * numLetters + CharIndex(char), 0);

    return [+row, col];    // row: String -> Number
}
```

💈範例：

```javascript
A1NotationToRowColumn("A2"),    // [ 2,  1 ]
A1NotationToRowColumn("AA3"),   // { 3, 27 ]
```

{% endtab %}

{% tab title="📗 參考" %}

* [x] [How to Convert Column Number (e.g. 28) to Column Letter (e.g. AB) in Google Sheets](https://www.labnol.org/convert-column-a1-notation-210601/) ⭐️ - row/column number ⇄ [A1 notation](/web/appendix/gas/classes/range/a1-notation.md)
  {% endtab %}

{% tab title="👥 相關" %}

* [A1Notation()](/web/appendix/gas/classes/range/a1-notation/a1notation.md) - (row, col) to [A1 notation](/web/appendix/gas/classes/range/a1-notation.md)
* [columnName()](/web/appendix/gas/classes/range/a1-notation/columnname.md)
* [str.match()](/web/js/val/prim/str/method/str.match.md)
* [destructuring array](/web/js/grammar/op/assign/destruct/arr.md)
  {% endtab %}
  {% endtabs %}
