2

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

  1. 班级(大一、大二、大三或大四)

我想创建一个插件,询问用户哪一列包含学生电子邮件地址列表,然后让它循环遍历该列,在每个学生电子邮件地址上运行每个函数,将数据分成单独的列。我对创建菜单项并为它们分配功能感到很自在。我觉得我需要一个很棒的 for 循环或三个。并且还需要构建一些逻辑和方法,让用户选择哪一列有电子邮件地址。下面是我的每个函数的代码。任何帮助将不胜感激!

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

}
4

2 回答 2

1

有:

getColumn()

Range 类的方法。

Google 文档 - 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 菜单中看到这些信息。

另外,学习如何使用调试器。

调试器和断点

于 2015-03-20T14:58:43.837 回答
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")))))
于 2015-03-20T15:05:10.750 回答