0

Basically is I have a sheet which control de name of other files :

Inside "source sheet" I have the cells in column A, which I put new names and cells in columns B, which contains the ID of the files I want to rename :

What I want is : each time I edit the names cell in column A, the file which belong to the ID chage the name, I mean rename.

To do this, I am implementing a Script in Google App Script which is the following:

This is the code :

function onEdit(e) {
  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  var edited_range = e.range;
  var edited_row = edited_range.getRow();
  var newName = spreadsheet.getActiveSheet().getRange(edited_row,1).getValue();
  var idDeBox = spreadsheet.getActiveSheet().getRange(edited_row,2).getValue();
  var file = SpreadsheetApp.openById(idDeBox);
  file.rename(newName);
}

But It doesn't work. It doesn't rename the files. I am struggling with this. What am I doing wrong ?

Update : I get this from execution logs :

enter image description here

NIN
  • 197
  • 13

1 Answers1

1

Try it like this:

Note: you are not actually specifying what cell you are actually editing.

function renameFile(e) {
  const sh = e.range.getSheet();
  var newName = sh.getRange(e.range.rowStart,1).getValue();
  var idDeBox = sh.getRange(e.range.rowStart,2).getValue();
  var file = SpreadsheetApp.openById(idDeBox);
  file.rename(newName);
}

and create an installable onEdit trigger with name of renameFile

Cooper
  • 59,616
  • 6
  • 23
  • 54
  • Thanks , Thanks. I really apreciate your help. This WORKS. . . . OMG. There came a time when I thought about giving up. Thanks Cooper. I never thought someone could help me with this. – NIN Nov 25 '22 at 23:41
  • Just One question. I dont really undestand when you say : ". . . and create an installable onEdit trigger with name of renamefile ". I did not do any of that. . . When I create the trigger , in the option: "choose which function to run" i put onEdit and in the option : "select even type" I put On edit. What do you mean with that ? – NIN Nov 26 '22 at 00:16
  • Sorry I meant to change the name of the function – Cooper Nov 26 '22 at 00:52
  • ok. Now I understand it. One last question : why Do I have to exchance the name "onEdit" for "renameFile" . It affects something to have the name "onEdit" as function name ? – NIN Nov 26 '22 at 01:07
  • If you don't then your function will receive both the simple trigger and the installable trigger which can cause problems that are difficult to trouble shoot – Cooper Nov 26 '22 at 01:39