3

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.

Mohamed H
  • 219
  • 2
  • 6
  • 13

1 Answers1

8

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/

Community
  • 1
  • 1
Brodie Garnet
  • 432
  • 2
  • 8
  • 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. – Mohamed H Aug 22 '15 at 11:58