Darkk
Darkk

Reputation: 13

How to change google sheets tab name depending on a cell with a date picker

I'm trying to make a sheet where when you change the text in a cell it changes the name of the current tab.

I currently have a simple script that works fine for just text, but I'm trying to use the built in date picker. For example when I type in "test" the tab is renamed to "test" but when I use the date picker and choose "4-6-22" it changes the tab name to "44657". Is there anyway to get it to rename the tab to just "4-6-22"?

This is the script I'm using

function onEdit(e) {
  if(e.range.getA1Notation() == 'B3'){
    SpreadsheetApp.getActiveSheet().setName(e.value);
  }
}

Upvotes: 1

Views: 724

Answers (2)

Mike Steelson
Mike Steelson

Reputation: 15318

If you want a specific format

function onEdit(e) {
  if(e.range.getA1Notation() == 'B3'){
    if (!isNaN(Date.parse(e.value))){
      SpreadsheetApp.getActiveSheet().setName(Utilities.formatDate(new Date(e.range.getDisplayValue()), Session.getScriptTimeZone(), "MMM dd yyyy"))
    }
    else{
      SpreadsheetApp.getActiveSheet().setName(e.value);
    }
  }
}

enter image description here

Upvotes: 1

Yuri Khristich
Yuri Khristich

Reputation: 14502

Probably this will work:

function onEdit(e) {
  if(e.range.getA1Notation() == 'B3'){
    SpreadsheetApp.getActiveSheet().setName(e.range.getDisplayValue());
  }
}

Upvotes: 0

Related Questions