Home > Mobile >  Dynamic sheet name change bases on a range
Dynamic sheet name change bases on a range

Time:02-11

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!

enter image description here

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])
  }
}

enter image description here

UPDATE: I'm trying to attach the function renameSheetTabs() into a drawing but for some reason I'm getting the following error.

enter image description here enter image description here enter image description here

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:

enter image description here

  • After running the script:

enter image description here

  • Execution logs for review

enter image description here

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])
  }
}
  • Related