Question 1.
I'm trying to write a script to rename and reorder sheets in a Google Spreadsheet based on a table on one of the sheets. I have been trying different methods for hours to get it to work to no avail. (I am still trying to get the hang of loops)
This script will be in a sheet that I will share with other people who may accidently reorder or possibly rename a sheet.
I have included the current code and a sample file.
NOTE: the sheet names will be completely different and it won't be possible to order them alphabetically.
function OnOpen(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheets = ss.getSheets();
for( var i in sheets )
if(sheets[i].getSheetId() == '978626951'){
var sheet = ss.getSheets()[i];
break;
}
var sheetData = sheet.getRange(2,1,11,3).getValues();
for (var a = 0; a < sheetData.length; a++) {
for (var b = 0; a < sheetData.length; b++){
var find = sheetData[a][0]; Logger.log(sheets[a].getSheetName());
if(find == sheets[a].getSheetId()) {
var temp = ss.getSheets()[a].activate();
ss.moveActiveSheet(sheetData[a][2]);
}
}
}
}
Link to sample spreadsheet with script.
Question No. 2
According to W3schools, it is possible to increment a loop from inside the loop. (code form W3)
var i = 0;
var len = cars.length;
for (; i < len; ) {
text += cars[i] + "<br>";
i++;
}
However, when I try to do this in Google Script the debugger hangs and I have to refresh. Is this not possible in Google script?
Any help would be greatly appreicated.