使用JavaScript

时间:2015-08-22 14:56:09

标签: javascript google-apps-script char conditional-statements auto-increment

我是脚本编程的初学者,我想写一个脚本,以便拥有一个自动ID(日期+大写字符)。

没有函数IF,脚本就可以了。但是,我的脚本仍然存在一些问题。我没有成功地使用函数IF的条件来增加角色。

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 du suffixe alphabétqiue pour l'ID
// If cells value (A.n)=(A.n-1) then character of cells "N.n" is incremented until "Z" (with n is number of LastRow)
if (SheetResponse.getRange(DerniereLigne,2).getValue() == SheetResponse.getRange(DerniereLigne-1,2).getValue()) {
        var AlphaNumber = SheetResponse.getRange(DerniereLigne-1,15).getValue().charCodeAt(0);
        var NextCode = AlphaNumber + 1;
        // Si Code (Z) alors restart to "A"
        if (NextCode > 90) {nextCode = 65;}
        var NextAlpha = String.fromCharCode( NextCode );
        }
// If not cells (N.n) is set to "A"
else {NextAlpha = "A";}

//Création de l'ID dans la derniére ligne et colonne "N"
SheetResponse.getRange(DerniereLigne,14).setValue(DateToday + NextAlpha);
SheetResponse.getRange(DerniereLigne,15).setValue(NextAlpha);

}

请有人帮助我。 提前谢谢。

2 个答案:

答案 0 :(得分:2)

这是一个以字典顺序返回下一个字符串的函数: 'A' - > 'B' - > ......'Z' - > 'AA' - > 'AB' - > 'AC' - > ......'AZ' - > 'BA' - > 'BB' - > ......'ZZ' - > 'AAA'等。

function nextString(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

}

答案 1 :(得分:0)

这是一个循环遍历字母表中所有大写字母的函数示例,然后递增一个字符:

function incrementAlpha() {
  var stringOfLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  var thisLoopAlpha = "",
      AlphaNumber = 1,
      NextCode = 0,
      NextAlpha = 0,
      thisCode = 0;

  for (var i=0;i<stringOfLetters.length;i+=1) {
    thisLoopAlpha = stringOfLetters[i];
    thisCode = thisLoopAlpha.charCodeAt(0);
    NextCode = thisCode + 1;
    // if Code (Z) then restart at "A"
    if (thisCode > 89) {
      NextCode = 65;
      NextAlpha = String.fromCharCode( NextCode );
    } else {
      //NextAlpha = "A";
      NextAlpha = String.fromCharCode( NextCode );
    };

    Logger.log('thisCode: ' + thisCode);
    Logger.log('NextCode: ' + NextCode);
    Logger.log('NextAlpha: ' + NextAlpha + "\n");
  };
};

我认为您的代码应该是这样构建的:

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 du suffixe alphabétqiue pour l'ID
  // If cells value (A.n)=(A.n-1) then character of cells "N.n" is incremented until "Z" (with n is number of LastRow)
  if (SheetResponse.getRange(DerniereLigne,2).getValue() == SheetResponse.getRange(DerniereLigne-1,2).getValue()) {
    var AlphaNumber = SheetResponse.getRange(DerniereLigne-1,15).getValue().charCodeAt(0);
    var NextCode = AlphaNumber + 1;

    // Si Code (Z) alors restart to "A"
    if (NextCode > 89) {
      nextCode = 65;
      var NextAlpha = String.fromCharCode( NextCode );
    } else {
      // If not cells (N.n) is set to "A"
      NextAlpha = "A";
    }; 
  }
  //Création de l'ID dans la derniére ligne et colonne "N"
  SheetResponse.getRange(DerniereLigne,14).setValue(DateToday + NextAlpha);
  SheetResponse.getRange(DerniereLigne,15).setValue(NextAlpha);
}