Google-apps-script function CHAR() and CODE()
12,534
CODE
:
var char = "A";
var number = char.charCodeAt(0);
// or simply:
"A".charCodeAt(0);
CHAR
:
var char = String.fromCharCode(34);
See also: Convert character to ASCII code in JavaScript and http://www.w3schools.com/jsref/jsref_fromcharcode.asp
So to increment:
// Uppercase letters goes from code 65 to 90
var nextCode = prevChar.charCodeAt(0) + 1;
if (nextCode > 90) {
nextCode = 65; // Start on A again. (just an example)
}
var next = String.fromCharCode( nextCode );
You'll need to know the ASCII table: http://www.asciitable.com/
Author by
Mohamed H
Updated on June 04, 2022Comments
-
Mohamed H almost 2 years
I am beginner in script programming and I would like to use the equivalent function CHAR() and CODE() in order to increment Alphabet characters according to an IF condition.
Example of formula I use in Google Sheets in the cells ("D11") "=IF(C11=C10;CHAR(CODE(A10)+1);"A")" .
I have written the following script:
function onFormSubmit(e) { //Déclaration des variables var SheetResponse = SpreadsheetApp.getActiveSheet(); var DerniereLigne = SpreadsheetApp.getActiveSheet().getLastRow(); var DateToday = Utilities.formatDate(new Date(), Session.getScriptTimeZone(), 'ddMMYY'); //Intégration dsu suffixe alphabétqiue pour l'ID if (SheetResponse.getRange(DerniereLigne,2).getValue() <> SheetResponse.getRange(DerniereLigne-1,2).getValue()) { var Alpha = SheetResponse.getRange(DerniereLigne,15).setValue("A"); } else { Alpha = SheetResponse.getRange(DerniereLigne,15).setValue(CHAR(CODE(Alpha)+1); } //Création de l'ID dans la derniére ligne et colonne "N" SheetResponse.getRange(DerniereLigne,14).setValue(DateToday + Alpha); }
Please, Could someone help me. Thank you in advance.