Reputation: 195
I would like to get the current month weeks number on this range C3:C8. Based on that values, to rename the following tabs names: Sheet3, Sheet4, Sheet5, Sheet6, Sheet7, Sheet8
Also, I would like this script to work only on sheet name: Sheet1
Is there a google script that can acomplish this request? I would really appreciate it. Thank you very much!
function sheetsNames(){
var ss = SpreadsheetApp.getActiveSpreadsheet()
var sh = ss.getSheetByName('Sheet1')
var values = sh.getRange('C3:C8').getValues()
for (var i=values.length-1;i>=0;i--){
ss.getSheets()[i+3].setName(values[i][0])
}
}
UPDATE: I'm trying to attach the function renameSheetTabs() into a drawing but for some reason I'm getting the following error.
Upvotes: 0
Views: 850
Reputation: 4048
You can try this script structure below:
function renameSheetTabs(){
var ss = SpreadsheetApp.getActiveSpreadsheet()
var sh = ss.getSheetByName('Sheet1')
var values = sh.getRange('C3:C8').getValues();
var startTab = 2; //first tab will be sheet #2 or the tab after "Sheet1"
for (var i=0; i<startTab;i++){
if(values.length == i)return; //loop will stop once all tabs have been renamed
Logger.log(ss.getSheets()[i+2].getName()+" To \""+values[i] +"\"\nRenaming Done!");
ss.getSheets()[i+2].setName(values[i]);
}catch{
return;
}
startTab += 1;
}
}
Upvotes: 1
Reputation: 15328
Try
function sheetsNames(){
var ss = SpreadsheetApp.getActiveSpreadsheet()
var sh = ss.getSheetByName('Sheet1')
var values = sh.getRange('C3:C8').getValues()
for (var i=values.length-1;i>=0;i--){
ss.getSheets()[i+3].setName(values[i][0])
}
}
Upvotes: 0
Reputation: 2261
I found this StackOverflow that has a code that answer this question:
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[1]; //Sheet 2
var cell = sheet.getRange("A2");//Sheet2!A2
var value = cell.getValue();//Added
sheet.setName(value);//Sheet2 name changed
}
Here is the Google Documentation.
Upvotes: 0