将自定义 Google 表格功能转变为附加组件
Turning custom Google Sheets functions into an add-on
我已经为 Google 表格编写了三个独立的函数。每个都将学生的电子邮件地址 (firstname.lastnameXX@d155.org) 作为输入,其中 XX 代表学生毕业年份,returns 三项信息之一。分别
- 名字
- 姓氏
- Class(大一、大二、大四)
我想创建一个附加组件,询问用户哪一列包含学生电子邮件地址列表,然后让它循环遍历该列,运行 每个学生电子邮件地址上的每个函数,将数据分成单独的列。我对创建菜单项并为它们分配功能感到很自在。我觉得我需要一个或三个很棒的 for 循环。并且还需要为用户构建一些逻辑和方法来 select 哪一列有电子邮件地址。下面是我的每个函数的代码。任何帮助将不胜感激!
//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;
}
有:
getColumn()
范围Class的方法。
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 菜单中看到这些信息。
此外,学习如何使用调试器。
您可以使用公式,或者如果您真的想创建一个附加组件,那么我建议让它以编程方式为您插入公式。
这是一个 sheet,他们在: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")))))
我已经为 Google 表格编写了三个独立的函数。每个都将学生的电子邮件地址 (firstname.lastnameXX@d155.org) 作为输入,其中 XX 代表学生毕业年份,returns 三项信息之一。分别
- 名字
- 姓氏
- Class(大一、大二、大四)
我想创建一个附加组件,询问用户哪一列包含学生电子邮件地址列表,然后让它循环遍历该列,运行 每个学生电子邮件地址上的每个函数,将数据分成单独的列。我对创建菜单项并为它们分配功能感到很自在。我觉得我需要一个或三个很棒的 for 循环。并且还需要为用户构建一些逻辑和方法来 select 哪一列有电子邮件地址。下面是我的每个函数的代码。任何帮助将不胜感激!
//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;
}
有:
getColumn()
范围Class的方法。
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 菜单中看到这些信息。
此外,学习如何使用调试器。
您可以使用公式,或者如果您真的想创建一个附加组件,那么我建议让它以编程方式为您插入公式。
这是一个 sheet,他们在: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")))))