我有一段时间发布在这里的代码,我最近开始使用Google电子表格稍微调整一下。然而,有两件事我无法完成:第一件事是从排列中排除主表(当我重新运行代码时我应该这样做,我不会重复)。这就是我试过的
var repArray = new Array();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var allSheets = ss.getSheets().not("Master";
它不起作用。
其次,为了按主列表排序第一列但从第二行开始(以防止标题行被排序)我尝试了这个
mSheet.getRange(2, 1, 1, 12).sort(2);
也没有运气。
以下是整个代码:
function onOpen() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var menuEntries = [ {name: "Consolidar Informacion", functionName: "updateMaster"}];
ss.addMenu("Mejoras", menuEntries);
}
function updateMaster() {
var repArray = new Array();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var allSheets = ss.getSheets().not("Master");
// build array of all sheets
for (i in allSheets) {
{repArray.push(allSheets[i].getName());}
}
// store all sheets in array
var sheetArray = [];
// loop through all rep sheets
for (var j in repArray) {
// get each sheet
var tempSheet = ss.getSheetByName(repArray[j]);
// get sheet data
var dataRange = tempSheet.getDataRange().getValues();
// remove the first header row
dataRange.splice(parseInt(0), 1);
// append sheet data to array
var sheetArray = sheetArray.concat(dataRange);
}
// Time to update the master sheet
var mSheet = ss.getSheetByName("Master");
// save top header row
var headerRow = mSheet.getRange(1,1,1,12).getValues();
// clear the whole sheet
SpreadsheetApp.getActiveSheet().getRange(2, 1, 1, 1).clear({contentsOnly: true});
// put back the header row
mSheet.getRange(1, 1, 1, 12).setValues(headerRow);
// write to the Master sheet via the array
mSheet.getRange(2, 1, sheetArray.length, 12).setValues(sheetArray);
// force spreadsheet updates
SpreadsheetApp.flush();
// pause (1,000 milliseconds = 1 second)
Utilities.sleep("200");
// Sorts the sheet by the first column, ascending
mSheet.getRange(2, 1, 1, 12).sort(2);
// delete empty rows at bottom
var last = mSheet.getLastRow();
var max = mSheet.getMaxRows();
if (last !== max) {mSheet.deleteRows(last+1,max-last);}
}
答案 0 :(得分:0)
这里有两个问题:
您需要主表的索引。说出它是第一张表:
var allSheets = ss.getSheets(); //get all the sheet
var indexOfMaster = 0; //or whichever the index is
var sheetsExceptMaster = allSheets.splice(indexOfMaster,1); //splice out 1 item at this index, which would be the master sheet
另一个Q是关于排序范围。此行并不反映您尝试做的事情:
mSheet.getRange(2, 1, 1, 12).sort(2);
它表示您想要一个深度为一行且宽度为12列的范围,然后对该范围的第二列进行排序。记住:
//getRange(x, y, number of rows, number of columns).sort(by which column);
根据你的解释,你可能想要的是这个:
mSheet.getRange(2, 1, mSheet.getLastRow, mSheet.getLastColumn).sort(1);
答案 1 :(得分:0)
所以在阅读了一下之后,不是代码不能正常运行它更像是Google Script的一个持续问题,不知怎么现在脚本正在运行,另外我不能让splice
工作所以我做的是首先从Master Senpai Sheet中刷出信息,然后只是得到整个安排,我的最终工作代码如下:
function onOpen() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var menuEntries = [ {name: "Consolidar Informacion", functionName: "updateMaster"}];
ss.addMenu("Mejoras", menuEntries);
}
function updateMaster() {
var repArray = new Array();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var allSheets = ss.getSheets();
var mSheet = ss.getSheetByName("Master Senpai")
mSheet.getRange(2, 1, mSheet.getLastRow(), mSheet.getLastColumn()).clear({contentsOnly: true});
// build array of all sheets
for (i in allSheets) {
{repArray.push(allSheets[i].getName());}
}
// store all sheets in array
var sheetArray = [];
// loop through all rep sheets
for (var j in repArray) {
// get each sheet
var tempSheet = ss.getSheetByName(repArray[j]);
// get sheet data
var dataRange = tempSheet.getDataRange().getValues();
// remove the first header row
dataRange.splice(parseInt(0), 1);
// append sheet data to array
var sheetArray = sheetArray.concat(dataRange);
}
// Time to update the master sheet
var mSheet = ss.getSheetByName("Master Senpai");
// save top header row
var headerRow = mSheet.getRange(1,1,1,12).getValues();
// clear the whole sheet
mSheet.getRange(2, 1, mSheet.getLastRow(), mSheet.getLastColumn()).clear({contentsOnly: true});
// put back the header row
mSheet.getRange(1, 1, 1, 12).setValues(headerRow);
// write to the Master sheet via the array
mSheet.getRange(2, 1, sheetArray.length, 12).setValues(sheetArray);
// force spreadsheet updates
SpreadsheetApp.flush();
// pause (1,000 milliseconds = 1 second)
Utilities.sleep("200");
// Sorts the sheet by the first column, ascending
mSheet.getRange(2, 1, mSheet.getLastRow(), mSheet.getLastColumn()).sort(1);
// Resize the columns
mSheet.autoResizeColumn(1);
mSheet.autoResizeColumn(2);
mSheet.autoResizeColumn(3);
mSheet.autoResizeColumn(4);
mSheet.autoResizeColumn(5);
mSheet.autoResizeColumn(6);
mSheet.autoResizeColumn(7);
mSheet.autoResizeColumn(8);
mSheet.autoResizeColumn(9);
mSheet.autoResizeColumn(10);
mSheet.autoResizeColumn(11);
mSheet.autoResizeColumn(12);
// delete empty rows at bottom
var last = mSheet.getLastRow();
var max = mSheet.getMaxRows();
if (last !== max) {mSheet.deleteRows(last+1,max-last);}
}
非常感谢。