将自定义Google表格功能转换为附加组件

时间:2015-03-20 13:30:45

标签: javascript google-apps-script google-apps-for-education

我为Google表格编写了三个单独的函数。每个人都将学生的电子邮件地址(firstname.lastnameXX@d155.org)作为输入,其中XX代表学生毕业年份,并返回三个信息中的一个。分别

  1. 名字
  2. 姓氏
  3. 班级(新生,Soph。初级或高级)
  4. 我想创建一个附加组件,询问用户哪个列包含学生电子邮件地址列表,然后让它循环遍历该列,在每个学生电子邮件地址上运行每个函数,将数据分成不同的列。我觉得创建菜单项并为它们分配功能感觉很舒服。我觉得我需要一个很棒的循环或三个。并且还需要构建一些逻辑和方法供用户选择哪个列具有电子邮件地址。以下是我的每个功能的代码。任何帮助将不胜感激!

       //Takes a student's email address and returns their proper first name. 
    function FNAME(input) {
      var length = input.length;
      var shortened = input.substring(0, length-11);
      var replaceSpace = shortened.replace('.', ' ');
      var spacePlace = replaceSpace.indexOf(" ");
      var fName = replaceSpace.charAt(0).toUpperCase() + replaceSpace.slice(1, spacePlace);
      return fName;
    
    }
    
    //Takes a student's email address and returns their proper last name. 
    function LNAME(input) {
      var length = input.length;
      var shortened = input.substring(0, length-11);
      var replaceSpace = shortened.replace('.', ' ');
      var spacePlace = replaceSpace.indexOf(" ");
      var lName = replaceSpace.charAt(spacePlace+1).toUpperCase() + replaceSpace.slice(spacePlace+2);
      return lName;
    
    }
    
    //Takes a student's email address and returns their class.
    function GLEVEL(input) {
      var currentTime = new Date();
      var length = input.length;
      var shortened = input.substring(0, length-9);
      var newLength = shortened.length;
      var gyear = input.substring(newLength-2, newLength);
      var year = currentTime.getYear();
      var month = currentTime.getMonth();
    
      if (year - gyear == 2000 && month <= 6) {
        gyear = "Senior";
      }
      else if (year - gyear == 1999 && month >= 7) {
        gyear = "Senior";
      }
    
      if (year - gyear == 1999 && month <= 6){
        gyear = "Junior";
      }
      else if (year - gyear == 1998 && month >= 7) {
        gyear = "Junior";
      }
    
      if (year - gyear == 1998 && month <= 6){
        gyear = "Sophomore"
      }
      else if (year - gyear == 1997 && month >= 7) {
        gyear = "Sophomore";
      }
    
      if (year - gyear == 1997 && month <= 6){
        gyear = "Freshman";
      }
      else if (year - gyear == 1996 && month >= 7) {
        gyear = "Freshman";
      }
      return gyear;  
    
    }
    

2 个答案:

答案 0 :(得分:0)

有:

getColumn()

范围类的方法。

Google Documentation - getColumn

function askUserToSelectColumn() {

  var thisSS = SpreadsheetApp.getActiveSpreadsheet();

  Browser.msgBox('Please click anywhere in the column that has the emails.  then click OK');

  var activeColumn = thisSS.getActiveCell().getColumn();

  var runCheck = Browser.msgBox('The active Column is Column x.  Run Program?', Browser.Buttons.OK_CANCEL);

  if (runCheck === 'ok') {
    loopThroughTheColumn(activeColumn); //Trigger function to run and pass the column number
  };  
};

处理列的功能:

function loopThroughTheColumn(argWhatColumn) {
  Logger.log('argWhatColumn: ' + argWhatColumn);
  var thisSS = SpreadsheetApp.getActiveSpreadsheet();
  var theSheet = thisSS.getSheetByName('theSheetNameToGet');

  //getRange(row, column, numRows, numColumns)
  var columnData = theSheet.getRange(2, argWhatColumn, 10, 1);
  Logger.log('columnData: ' + columnData);

  for (i=0;i<columnData.length;i++) {
    //Code for each row in the column
  };
};

请注意Logger.log()语句的使用。 Logger.log()语句将信息打印到LOGS,然后可以从VIEW,LOGS菜单中看到。

另外,了解如何使用调试器。

Debugger and Breakpoints

答案 1 :(得分:0)

您可以使用公式,或者如果您真的想要创建附加组件,那么我建议您以编程方式为您插入公式。

以下是包含在https://docs.google.com/spreadsheets/d/1iGwlZ6w8M8O5ye8Sjg8dxQmSTbB6ilT5ZIG9J-9LW80/edit?usp=sharing

中的工作表
colA: John.doe15@d155.org
colB: =left(A2,search(".",A2)-1)   ~ result: John
colC: =mid(A2,search(".",A2)+1,search("@",A2)-search(".",A2)-3)   ~ result: doe
colD: =mid(A2,search("@",A2)-2,2)   ~ result: 15
colE: =if(D2="15","Senior",if(D2="16","Junior",if(D2="17","Sophmore",if(D2="18","Freshmen"))))  ~ result: Senior

你也可以用arrayFormula()函数包围它,它会自动填充整个列。

colA: John.doe15@d155.org
B2: =arrayFormula(left(A2:A10,search(".",A2:A10)-1))
C2: =arrayFormula(mid(A2:A10,search(".",A2:A10)+1,search("@",A2:A10)-search(".",A2:A10)-3))
D2: =arrayFormula(mid(A2:A10,search("@",A2:A10)-2,2))
E2: =ArrayFormula(if(D2:D10="15","Senior",if(D2:D10="16","Junior",if(D2:D10="17","Sophmore",if(D2:D10="18","Freshmen")))))