# A1NotationToRowColumn()

[GAS](https://lochiwei.gitbook.io/web/appendix/gas) ⟩ [Classes](https://lochiwei.gitbook.io/web/appendix/gas/classes) ⟩ [Range](https://lochiwei.gitbook.io/web/appendix/gas/classes/range) ⟩ [A1 notation](https://lochiwei.gitbook.io/web/appendix/gas/classes/range/a1-notation) ⟩ 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 ⇄ [](https://lochiwei.gitbook.io/web/appendix/gas/classes/range/a1-notation "mention")
  {% endtab %}

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

* [a1notation](https://lochiwei.gitbook.io/web/appendix/gas/classes/range/a1-notation/a1notation "mention") - (row, col) to [](https://lochiwei.gitbook.io/web/appendix/gas/classes/range/a1-notation "mention")
* [columnname](https://lochiwei.gitbook.io/web/appendix/gas/classes/range/a1-notation/columnname "mention")
* [str.match](https://lochiwei.gitbook.io/web/js/val/prim/str/method/str.match "mention")
* [arr](https://lochiwei.gitbook.io/web/js/grammar/op/assign/destruct/arr "mention")
  {% endtab %}
  {% endtabs %}
