Is there a Google Sheets formula to put the name of the sheet into a cell?

后端 未结 9 1398
轮回少年
轮回少年 2020-12-30 18:59

The following illustration should help:

9条回答
  •  無奈伤痛
    2020-12-30 19:20

    Here is my proposal for a script which returns the name of the sheet from its position in the sheet list in parameter. If no parameter is provided, the current sheet name is returned.

    function sheetName(idx) {
      if (!idx)
        return SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getName();
      else {
        var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
        var idx = parseInt(idx);
        if (isNaN(idx) || idx < 1 || sheets.length < idx)
          throw "Invalid parameter (it should be a number from 0 to "+sheets.length+")";
        return sheets[idx-1].getName();
      }
    }
    

    You can then use it in a cell like any function

    =sheetName() // display current sheet name
    =sheetName(1) // display first sheet name
    =sheetName(5) // display 5th sheet name
    

    As described by other answers, you need to add this code in a script with :

    Tools > Script editor
    

提交回复
热议问题