Skip to content

Instantly share code, notes, and snippets.

@mark05e
Last active July 20, 2022 23:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mark05e/9a20224a3b262256409f9030b28c7276 to your computer and use it in GitHub Desktop.
Save mark05e/9a20224a3b262256409f9030b28c7276 to your computer and use it in GitHub Desktop.
// check if we are inside GAS
// source - https://github.com/WildH0g/UnitTestingApp/blob/master/UnitTestingApp.js
function inGas() {
return typeof ScriptApp !== 'undefined';
}
// get the sheet ID from a url
// source - https://stackoverflow.com/questions/16840038/easiest-way-to-get-file-id-from-url-on-google-apps-script
function getIdFromUrl(url) {
try {
return url.match(/[-\w]{25,}/)[0];
} catch (error) {
return null
}
}
// get the next alphabet to use in a sheet
// source - https://stackoverflow.com/questions/32157500/increment-alphabet-characters-to-next-character-using-javascript
function nextAlphabetString(str) {
if (! str)
return 'A' // return 'A' if str is empty or null
let tail = ''
let i = str.length -1
let char = str[i]
// find the index of the first character from the right that is not a 'Z'
while (char === 'Z' && i > 0) {
i--
char = str[i]
tail = 'A' + tail // tail contains a string of 'A'
}
if (char === 'Z') // the string was made only of 'Z'
return 'AA' + tail
// increment the character that was not a 'Z'
return str.slice(0, i) + String.fromCharCode(char.charCodeAt(0) + 1) + tail
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment