简体   繁体   中英

Google Apps Script: 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?

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 }

Just thought I'd add this as well. You can also add a trigger to any function since next to play & debug there is an option for triggers which you can customize to any function.

Here is a link to the image of the button

var workBookName = SpreadsheetApp.getActiveSpreadsheet().getName();
var someVariableName = sheet.getRange("G3").getValue();

var newWorkbookName = sheet.getRange("F3").getValue() + "_ConcatString_" + someVariableName // if you want to concat some values
Logger.log(workBookName);
Logger.log(newWorkbookName);
if (newName.toString().length>0 && workBookName !== newWorkbookName) {
  workBook.rename(newWorkbookName);
}

You can use the onOpen trigger , so the cell will update each time your file is open:

function onOpen(e) {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();
  sheet.getRange("A1").setValue(ss.getName());  
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM