4

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.

1 Answer 1

9

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/

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you @Brodie Garnet for your reactivity and the information links. The function code and char is OK. However, I get some issues with my script , I don't succeed to increment the character with the condition of function IF.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.