我有一个运行三个函数的脚本(组合)。首先,它将表单响应拆分为行(拆分),然后它应该按字母顺序(排序)对响应进行排序,最后删除包含特定列中空白单元格的任何行。我使用脚本相当新,但我认为我有这个工作。现在,当我尝试运行它或调试时,它会给出" ReferenceError:" sort"没有定义。 (第3行,文件"合并")。我确定这是一个相当简单的解释。我为此设置了触发器,以便在表单提交,更改和打开时运行。
是否有人愿意审核并提出建议?其余部分似乎正在发挥作用。
function combined() {
SPLIT();
sort();
deleteRows();
function SPLIT() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sh0 = ss.getSheets()[0], sh1 = ss.getSheets()[1];
// get data from sheet 2
var data = sh1.getDataRange().getValues();
// create array to hold data
var aMain = new Array();
// itterate through data and add to array
// j=3 is the column it starts to loop with, j<9 tells it where to stop.
// in the aMain.push line, use data[i][j] for the rows to search and put in the one column.
// in the last line, change the last number to equal the number of columns in your final sheet.
// the first number in getrange is the row the data starts on... not sure about the 1.
for(var i=1, dLen=data.length; i<dLen; i++) {
for(var j=5; j<9; j++) {
aMain.push([data[i][0],data[i][1],data[i][2],data[i][3],data[i][4],data[i][j]]);
}
// add array of data to first sheet
sh0.getRange(2, 1, aMain.length, 6).setValues(aMain);
}
function sort() {
/** Variables for customization:
Each column to sort takes two variables:
1) the column index (i.e. column A has a colum index of 1
2) Sort Asecnding -- default is to sort ascending. Set to false to sort descending
**/
//Variable for column to sort first
var sortFirst = 3; //index of column to be sorted by; 1 = column A, 2 = column B, etc.
var sortFirstAsc = true; //Set to false to sort descending
//Variables for column to sort second
var sortSecond = 2;
var sortSecondAsc = true;
//Variables for column to sort third
var sortThird = 6;
var sortThirdAsc = true;//Number of header rows
var headerRows = 1;
/** End Variables for customization**/
/** Begin sorting function **/
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sh0 = ss.getSheets()[0];
var range = sh0.getRange(headerRows+1, 1, sh0.getMaxRows()-headerRows, sh0.getLastColumn());
range.sort([{column: sortFirst, ascending: sortFirstAsc}, {column: sortSecond, ascending: sortSecondAsc}, {column: sortThird, ascending: sortThirdAsc},]);
function deleteRows() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
// get data from the first sheet - the first sheet start with 0, we can also name a specific sheet by name but that's a little different setup.
var sh0 = ss.getSheets()[0];
var rows = ss.getDataRange();
var numRows = rows.getNumRows();
var values = rows.getValues();
var rowsDeleted = 0;
for (var i = 0; i <= numRows - 1; i++) {
var row = values[i];
if (row[5] == 'delete' || row[5] == '') { // This searches all cells in column (change to row[1] for columns B and so on) and deletes row if cell is empty or has value 'delete'.
sh0.deleteRow((parseInt(i)+1) - rowsDeleted);
rowsDeleted++;
}
}
}
}
}
}
答案 0 :(得分:1)
请不要忘记为您的帐户https://script.google.com/u/1/home/usersettings启用“ API GOOGLE APP SCRIPT”。
如果“ API GOOGLE APP SCRIPT”已关闭:
答案 1 :(得分:0)
缺少结束括号
function sort() {
/** Variables for customization:
Each column to sort takes two variables:
1) the column index (i.e. column A has a colum index of 1
2) Sort Asecnding -- default is to sort ascending. Set to false to sort descending
**/
//Variable for column to sort first
var sortFirst = 3; //index of column to be sorted by; 1 = column A, 2 = column B, etc.
var sortFirstAsc = true; //Set to false to sort descending
//Variables for column to sort second
var sortSecond = 2;
var sortSecondAsc = true;
//Variables for column to sort third
var sortThird = 6;
var sortThirdAsc = true;//Number of header rows
var headerRows = 1;
/** End Variables for customization**/
/** Begin sorting function **/
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sh0 = ss.getSheets()[0];
var range = sh0.getRange(headerRows+1, 1, sh0.getMaxRows()-headerRows, sh0.getLastColumn());
range.sort([{column: sortFirst, ascending: sortFirstAsc}, {column: sortSecond, ascending: sortSecondAsc}, {column: sortThird, ascending: sortThirdAsc},]);
}