2013-12-18 53 views
1

這是一個程序,用於在1張包含幾張表格的電子表格中移動數據 我對JavaScript沒有太多瞭解我只學過Java。Google SpreadsheetApp需要幫助

我的問題:

addAcceptedHours方法被設計成移動單元與多個數據,以單獨的片材(含有在第一列名的列表,列2-5包含數字)。我需要它將小時移動到行的某一列(該行從searchCol(String, Sheet)給出)。 該var data是從一種形式提交的信息2D陣列看起來像這樣:

[[(new Date(1339776313000)), "Firstname last", "[email protected]", 2015 , "A paragraph of text.", another paragraph of text", 00, "Freshman", "yet another paragraph of text", "Even moar text", "more text :3", "Firstname last"]] 


     0=timestamp 
     1=**firstname last** 
     2=email 
     3=** int(year graduating)** 
     4= paragraph of text (irrelevant to part) 
     5=** float (a number of hours) 
     6= "freshman", "sophomore", "junior", or "senior" (this will determine the column the number from 5 goes)** 
     7= paragraph of text (irrelevant to part) 
     8=paragraph of text (irrelevant to part) 
     9=paragraph of text (irrelevant to part) 
     10=paragraph of text (irrelevant to part) 
     11=a name onceagain 

我需要添加(5)的數量,以一個行中的另一個片材的2-5列取決於( 6) 由於某種原因,它跳過一切addAcceptedHours if語句後添加小時進入第2列每

//made with `enter code here`Google SpreadsheetApp Scripts 
//PUBLIC VARIABLES 
    var data; 
//please ignore missing methoods 
function onOpen(){ 
    var menu = [{name: 'Manage Hours', functionName: 'run'}]; 
    SpreadsheetApp.getActive().addMenu('Manage Hours', menu); 
} 
//starts the program 
function run(){ 
    log('open'); 
    getNext(); 
} 

//opens window with options 
function getNext(){ 
    data = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Pending Hours').getSheetValues(2, 1, 1, 12); 
    var cont = Browser.msgBox('Manage Hours', 'Would you like to get the next submission?', Browser.Buttons.YES_NO); 
    if (cont == 'yes'){ 
    { 
     var msg = data[0][1] + "\\nClass of " + data[0][3] + "\\n Submitted " + data[0][6] + " Hours for their " + data[0][7] + " Year." + "\\nDescription of Service:\\n" + data[0][4] + "\\nContact Information:\\n" + data[0][5] + "\\nRecommendation: " + data[0][8] + "\\n" + data[0][9] + "\\n\\nAccept?"; 
     var ans = Browser.msgBox('Manage Hours', msg , Browser.Buttons.YES_NO_CANCEL); 
     log(ans); 
    } 
     if (ans == 'yes') 
     accept(); 
     else if (ans == 'no') 
     deny(); 
    else cancel(); 
    } 
    else 
    log("close"); 
} 

function accept(){ 
    addAcceptedHours();//adds hours to accepted place 
    addChesedOpportunity();//adds to opportunitys 
    archive('yes');//archives the row 
    mailUser('yes');//mails user Accpeted 
    SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Pending Hours').deleteRow(2);//deletes row for next use 
    getNext();//starts over 
} 


function addAcceptedHours() { 
    //get data 
    var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Pending Hours'); 
    var sss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Accepted Hours'); 
    var numhr = parseFloat(ss.getRange(2, 7).getValue().toString()); 
    //asks how many hours to accept 
    var hrs = Browser.inputBox("how many hours will you accept (out of " + numhr + " hours)", Browser.Buttons.OK); 
    if (hrs.length > 0){ 
    numhr = parseFloat(hrs); 
    ss.getRange(2, 7).setValue(numhr);} 
    //finds the row with the students name to add hours to 
    var name = data[0][1]; 
    var row = searchCol(name,sss); 
    if (row == -1)// if not found, look to see if simaler names (check for misspelled names), then if the name is the same person set row to that row 
    row = checkForSim(name); 
    if (row == -1) {//if not misspelled then add the students name to the list, put it in order, get the row # 
    addStudent(name); 
    sss.sort(1); 
    row = searchCol(name,sss);} 

    //finds wich column to add years to 
    var col = 2; 
    if (data[0][8] == 'Sophmore') 
    col=3; 
    else if (data[0][8] == 'Junior') 
    col=4; 
    else if (data[0][8] == 'Seinor') 
    col=5; 
    //sets info for hours 
    sss.getRange(row, col).setValue(parseFloat(sss.getRange(row, col).getValue()) + numhr); 
} 


function searchCol(str, ss){//returns the row "str" is found in the first column of the sheet("ss") 
    var data = ss.getRange(1, 1, ss.getLastRow()+1, 1).getValues(); 
    var found = false; 
    for (var i=0; i < data.length;i++){ 
    if (data[i].toString().equalsIgnoreCase(str)){ 
     found = true; 
     return i+1;}} 
    return -1; 
} 

function checkForSim(name){//looks for simaler names, and suggestes them, if they say it is the same person, return that row # 
    var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Accepted Hours'); 
    var count =0; 
    for (var x=2; x < ss.getLastRow(); x++){ 
    for (var i=0; i<ss.getRange(x, 1).getValue().toString().length - 2; i++){ 
     if (ss.getRange(x, 1).getValue().toString().substr(i,i+2) == name.substr(i,i+2)) 
     count++;} 
    if (count > 0 && Browser.msgBox('Is this the same person?', ss.getRange(x, 1).getValue().toString() + '\nAnd,\n' + name , Browser.Buttons.YES_NO) == 'yes') 
     return x; 
    count =0;} 
    return -1; 
} 

function addStudent(name){ 
    var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Accepted Hours'); 
    var row = ss.getLastRow(); 
    ss.getRange(row, 1).setValue(name); 
    for (var x = 2; x<5;x++){ 
    ss.getRange(row, x).setValue(0); 
    } 
    ss.sort(1); 
} 

回答

0

固定時間:

function addAcceptedHours() { 
    //get data 
    var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Pending Hours'); 
    var sss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Accepted Hours'); 
    var numhr = parseFloat(ss.getRange(2, 7).getValue().toString()); 

    //asks how many hours to accept 
    var hrs = Browser.inputBox("how many hours will you accept (out of " + numhr + " hours)", Browser.Buttons.OK); 
    if (hrs.length > 0){ 
    numhr = parseFloat(hrs); 
    ss.getRange(2, 7).setValue(numhr);} 
    data[0][6]=numhr; 
    //finds the row with the students name to add hours to 
    var name = data[0][1]; 
    var row = searchCol(name,sss); 
    if (row == -1)// if not found, look to see if simaler names (check for misspelled names), then if the name is the same person set row to that row 
    row = checkForSim(name); 
    if (row == -1) {//if not misspelled then add the students name to the list, put it in order, get the row # 
    addStudent(name); 
    sss.sort(1); 
    row = searchCol(name,sss);} 
    //finds wich column to add years to 
    var col = 2; 
    if (data[0][7].toString() == 'Freshman') 
    col=2; 
    else { 
    if (data[0][7].toString() == 'Sophmore') 
     col=3; 
    else{ 
     if (data[0][7].toString() == 'Junior') 
     col=4; 
     else{ 
     if (data[0][7].toString() == 'Senior') 
      col=5; 
     }}} 
    sss.getRange(row, col).setValue(parseFloat(sss.getRange(row, col).getValue()) + numhr); 
} 

function addChesedOpportunity(){ 
    //gets info 
    var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Chesed Opportunities') 
    var private = data[0][11]; 
    //if private? is not awnsered, ask if they want it private 
    if (private==null) 
     private = Browser.msgBox("Would you like to make this information Private?",'Name:\\n' + data[0][2] + '\\nDescription of Service:' + data[0][5] + '\\nReccomends:' + data[0][9] + ' ' + data[0][10], Browser.Buttons.YES_NO); 
    //adds opportunity to oportunity sheet if the info is not private 
    if (private.equalsIgnoreCase('no')){ 
    var rec = data[0][9] + " " + data[0][10]; 
    var row = ss.getLastRow(); 
    ss.getRange(row, 1).setValue(data[0][2]); 
    ss.getRange(row, 2).setValue(data[0][5]); 
    ss.getRange(row, 3).setValue(rec); 
    ss.getRange(row, 4).setValue(data[0][6]);} 
}