JSFiddle - React, Tailwind, and code Playground
JavaScript
/**
* Converting colum letter to column index. Start of column index is 0.
* Ref: https://tanaikech.github.io/2022/05/01/increasing-column-letter-by-one-using-google-apps-script/
* @param {String} letter Column letter.
* @return {Number} Column index.
*/
function columnLetterToIndex_(letter = null) {
letter = letter.toUpperCase();
return [...letter].reduce((c, e, i, a) => (c += (e.charCodeAt(0) - 64) * Math.pow(26, a.length - i - 1)), -1);
}
/**
* Converting colum index to column letter. Start of column index is 0.
* Ref: https://stackoverflow.com/a/53678158
* @param {Number} index Column index.
* @return {String} Column letter.
*/
function columnIndexToLetter_(index = null) {
return (a = Math.floor(index / 26)) >= 0 ? columnIndexToLetter_(a - 1) + String.fromCharCode(65 + (index % 26)) : "";
}
/**
* Converting a1Notation to gridrange.
* Ref: https://tanaikech.github.io/2017/07/31/converting-a1notation-to-gridrange-for-google-sheets-api/
* @param {String} a1Notation A1Notation of range.
* @param {Number} sheetId Sheet ID of the range.
* @return {Object} Gridrange.
*/
function convA1NotationToGridRange_(a1Notation, sheetId) {
const { col, row } = a1Notation.toUpperCase().split("!").map(f => f.split(":")).pop().reduce((o, g) => {
var [r1, r2] = ["[A-Z]+", "[0-9]+"].map(h => g.match(new RegExp(h)));
o.col.push(r1 && columnLetterToIndex_(r1[0]))
o.row.push(r2 && Number(r2[0]))
return o;
}, { col: [], row: [] });
col.sort((a, b) => a > b ? 1 : -1);
row.sort((a, b) => a > b ? 1 : -1);
const [start, end] = col.map((e, i) => ({ col: e, row: row[i] }));
const gridrange = {
sheetId,
startRowIndex: start?.row && start.row - 1,
endRowIndex: end?.row ? end.row : start.row,
startColumnIndex: start && start.col,
endColumnIndex: end ? end.col + 1 : 1,
};
if (gridrange.startRowIndex === null) {
gridrange.startRowIndex = 0;
delete gridrange.endRowIndex;
}
if (gridrange.startColumnIndex ===...