Search code examples
google-sheetsgoogle-apps-scriptgoogle-sheets-formulacustom-function

Update cell to file name


I'm trying to update a cell to the files name. I created a simple script with the following

function fileName() {
  return SpreadsheetApp.getActiveSpreadsheet().getName();
}

This works by setting a cell to =fileName()

However, if I change the file name this does not update. How can I get this update to happen when the file name changes?


Solution

  • Here is a way to do it without a formula. anytime the sheet is edited it will update the cell.

    function onEdit(e) {
      var ss = SpreadsheetApp.getActiveSpreadsheet();
      var ssName = ss.getName();
      ss.getRange('A1:A1').setValue(ssName);  //change A1:A1 to whatever cell you would like
    }