需要Google SpreadsheetApp帮助

时间:2013-12-18 18:09:03

标签: google-apps-script google-sheets

这是一个旨在在包含几张表格的1个电子表格中移动数据的程序 我不太了解我只学习Java的JavaScript。

我的问题:

addAcceptedHours方法旨在将包含数字数据的单元格移动到单独的工作表(包含第一列中的名称列表,第2-5列包含数字)。我需要它将小时数移动到行的某一列(该行是从searchCol(String, Sheet)给出的)。 var data是从表单提交的信息的二维数组,如下所示:

[[(new Date(1339776313000)), "Firstname last", "email@email.com", 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语句之后的所有内容,并且每次都会将小时数添加到第二列

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

1 个答案:

答案 0 :(得分: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]);}
}