2013-03-14 3 views
1

나는 코드 작성에 매우 익숙하며 제한된 이해가 있으므로 부드럽게하십시오! this question에서 만든 코드를 확장하려고했습니다.자동 증가 작업 참조

나는 제한된 성공을 거두었고, 지금은 붙어있어, 누군가가 나를 올바른 방향으로 가르키거나 내가 뭘 잘못하고 있는지를 충분히 알기를 바랐다.

그래서 시나리오 : A는 내가 일하는 IT 부서에 netbook 수리를 예약하기 위해 자동 증가 "Job Reference"가 있어야합니다. 이 예약은 Google 양식을 통해 이루어지며 완벽하게 작동하려면 링크에서 코드를 가져올 수 있습니다. 나는 간단한 카운트보다 조금 더 많은 것을 원했습니다 - 1,2,3,4,5 등등. 이상적으로 작업 참조는 JR0001, JR0002 등으로 표시됩니다.

코드에서 시도했습니다.

완벽하게 작동하는 사용자 : 자신이 제출 한 코드.

내가 시도하는 첫번째 일은 단순히 다른 변수를 추가하고이 "JR1"대신 1을 받고으로까지 근무하지만, 자동 증가가 중지 .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); 
    } 
} 

에 추가했다

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); 
    } 
} 

제출 된 모든 양식에 대해 작업했기 때문에 나는 여전히 "JR1"을 가지고있었습니다. 실제로 자동 증가가 아닙니다!

나는 다음 작업 참조

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); 
    } 
} 

같은 결과로 시트에서 다른 .getValue을 설정하는 시도 - 비 증분 "JR1을"

그때 내 작업과 작업 증가 수를 합치려고

심판 세포로 보낸 다음 스크립트에서이를 호출합니다. 값이 내가 다른 변수를 추가하고 증가 번호로 앞에 0을 추가하는 방법을 이해하지 않는 경우 수가 증가 중지 이유를 이해하지 않습니다

증가하지 않는

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를 제출합니다.

나는 가능한 한 잘못 가고있는 부분에 대한 조언을 정말하고 싶습니다. 배우고 싶지만 분명히 핵심 원칙이 빠져 있습니다.

답변

3

여기 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. 
} 
+0

당신 선생님이 천재를 사용하는 작업 솔루션입니다! –

+0

ou 선생님은 천재입니다! 나는 당신의 코드를 조금 편집했다. 행 12 나는 Job Ref가 양식과 같은 행에 채워지도록 문자열을 'JR % 04d'로 변경하여 +1 문자 2 개와 4 개의 숫자를 갖도록 +1을 제거했다. 나는 9999 대의 노트북을 수리 할 필요가 결코 없기를 바란다. D –

+0

나는 적어도 1 백만을 수리 할 수 ​​있다고 생각했다. 나는 매우 실망한다 .-D - 대답을 받아들이는 것을 고려해보고, 투표에 감사한다. –