1

私はコードを書くのがとても新しく、理解が限られているので、優しくしてください!私はこの質問で作成されたコードを拡張しようとしています。

私は限られた成功しか収めておらず、今は行き詰まっています。誰かが私を正しい方向に向けたり、私が間違っていることを教えてくれるほど親切にしてくれることを望んでいました。

したがって、シナリオ:私が働いているIT部門のネットブック修理で予約するために、自動インクリメントの「ジョブリファレンス」が必要です。この予約はGoogleフォームを介して行われ、リンクのコードを完全に機能させることができますが!私は単純な数より少し多く(1、2、3、4、5など)を持っていることを望んでいました。理想的には、ジョブ参照はJR0001、JR0002などとして表示されます。

だから、私のコードの試み!

ユーザーが提出したコードで、完全に機能します。

function onFormSubmit(e) {
  // Get the active sheet
  var sheet = SpreadsheetApp.getActiveSheet();
  // Get the active row
  var row = sheet.getActiveCell().getRowIndex();
  // Get the next ID value.  NOTE: This cell should be set to: =MAX(A:A)+1
  var id = sheet.getRange("P1").getValue();
  // Check of ID column is empty
  if (sheet.getRange(row, 1).getValue() == "") {
    // Set new ID value
    sheet.getRange(row, 1).setValue(id);
  }
}

私が最初に試したのは、単に別の変数を追加して、それを.setValueに追加することでした。

function onFormSubmit(e) {
   // Get the active sheet
  var sheet = SpreadsheetApp.getActiveSheet();
  // Get the active row
  var row = sheet.getActiveCell().getRowIndex();
  // Get the next ID value.  NOTE: This cell should be set to: =MAX(A:A)+1
  var id = sheet.getRange("X1").getValue();
  // Set Job Reference
  var jobref = "JR";
  // Check of ID column is empty
  if (sheet.getRange(row, 1).getValue() == "") {
  // Set new ID value
    sheet.getRange(row, 1).setValue(jobref+id);
  }
}

これは、1ではなく「JR1」を取得するまでは機能しましたが、自動インクリメントが機能しなくなったため、送信されたすべてのフォームに対して、「JR1」が残っていました。実際には自動インクリメントではありません。

次に、シートから別の.getValueをジョブ参照として設定してみました

function onFormSubmit(e) {
   // Get the active sheet
  var sheet = SpreadsheetApp.getActiveSheet();
  // Get the active row
  var row = sheet.getActiveCell().getRowIndex();
  // Get the next ID value.  NOTE: This cell should be set to: =MAX(A:A)+1
  var id = sheet.getRange("X1").getValue();
  // Set Job Reference
  var jobref = sheet.getRange("X2").getValue();
  // Check of ID column is empty
  if (sheet.getRange(row, 1).getValue() == "") {
  // Set new ID value
    sheet.getRange(row, 1).setValue(jobref+id);
  }
}

同じ結果-増分しない「JR1」

次に、作業増分番号をジョブ参照セルと連結して、スクリプトで呼び出してみました。

function onFormSubmit(e) {
  // Get the active sheet
  var sheet = SpreadsheetApp.getActiveSheet();
  // Set Job Reference
  var jobref = sheet.getRange("X2").getValue();
  // Get the active row
  var row = sheet.getActiveCell().getRowIndex();
  // Get the next ID value.  NOTE: This cell should be set to: =MAX(A:A)+1
  var id = sheet.getRange("X1").getValue();
  // Check of ID column is empty
  if (sheet.getRange(row, 1).getValue() == "") {
  // Set new ID value
    sheet.getRange(row, 1).setValue(jobref);
  }
}

同じ結果、値は増加しません

他の変数を追加した場合に数値の増分が停止する理由がわかりません。また、増分する数値に先行ゼロを追加する方法もわかりません。物事を複雑にしすぎているような気がします!

したがって、要約すると、6文字の長さの自動インクリメント参照を取得する方法があります。このシナリオでは、最初にJR0001をフォームし、次にJR0002を送信します。

可能であれば、どこが間違っているのかについてのいくつかの指針が本当に欲しいです。学びたいのですが、明らかにいくつかの重要な原則が欠けています。

4

1 に答える 1

3

これは、GASチームが数日前に追加したばかりの「真新しい」を使用する実用的なソリューションですUtilities.formatString();-)

function OnForm(){
var sh = SpreadsheetApp.getActiveSheet()
var startcell = sh.getRange('A1').getValue();
if(! startcell){sh.getRange('A1').setValue(1);return}; // this is only to handle initial situation when A1 is empty.
var colValues = sh.getRange('A1:A').getValues();// get all the values in column A in an array
var max=0;// define the max variable to a minimal value
  for(var r in colValues){ // iterate the array
    var vv=colValues[r][0].toString().replace(/[^0-9]/g,'');// remove the letters from the string to convert to number
    if(Number(vv)>max){max=vv};// get the highest numeric value in th column, no matter what happens in the column... this runs at array level so it is very fast
    }
    max++ ; // increment to be 1 above max value
sh.getRange(sh.getLastRow()+1, 1).setValue(Utilities.formatString('JR%06d',max));// and write it back to sheet's last row.
}
于 2013-03-15T09:25:15.763 に答える