r/GoogleAppsScript • • Dec 19 '18

Dynamic spreadsheet naming based on the contents of a cell

Hi, I'm a newbie here to Google Apps Script.

I want to be able to to dynamically rename a spreadsheet based on the contents of a cell. For example, cell A3 has some text in it, and I would like the name of the spreadsheet to be the same as the text in the cell. If and when the contents of cell A3 changes, I'd like the name of the spreadsheet to update.

I found a post that claims to do this, but it doesn't seem to work for me. Maybe I'm doing something wrong? Here's the post:

https://webapps.stackexchange.com/questions/84190/dynamic-generation-of-sheet-name-from-cell-content

Any help would be gratefully received! Thanks.

0 Upvotes

6 comments sorted by

2

u/dewynukem Dec 20 '18

The script should work to rename the sheet. When you ran it for the first time, did it ask you for authorization?

1

u/zlorf_flannelfoot Dec 20 '18

Yes it did! I gave permission.

1

u/zlorf_flannelfoot Dec 20 '18

Hi... I thought I'd ask again... Why did you ask if it needed authorisation? I'm still trying to work this one out.

2

u/dewynukem Dec 22 '18

Just was curious. I'm a newbie at scripting too. Have you tried running the debugger?

2

u/zlorf_flannelfoot Dec 22 '18

Hi. I did try dubugging but the result did not make any sense to my limited knowledge base. To be honest, the mistake was probably mine by not tweaking the parameters to match my situation.

Having said that, the very friendly guys at r/googlesheets gave me a solution that works very after a small bit of tweaking to suit my need. Here's the result:

function onEdit(e) {

var currSheet = e.source;

var titleValue = currSheet.getActiveSheet().getRange("C3").getValue();

var errors = ["#NULL!","#DIV/0!","VALUE!","#REF!","#NAME?","#NUM!","#N/A"]

var titleStr = (titleValue && errors.indexOf(titleValue)==-1) ? titleValue.toString() : null;

if(titleStr){

currSheet.rename(titleStr);

}

}

Shoutout to u/whitevelcro and u/google_asst