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.
CodePudding user response:
Answer
You can try this script structure below:
Script
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;
}
}
Sample Demonstration
- Test Sheet:
- After running the script:
- Execution logs for review
CodePudding user response:
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.
CodePudding user response:
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])
}
}