> For the complete documentation index, see [llms.txt](https://lochiwei.gitbook.io/web/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://lochiwei.gitbook.io/web/appendix/gas/app/member/app.sheetbyname.md).

# app.sheetByName()

[GAS](/web/appendix/gas.md) ⟩ [app](/web/appendix/gas/app.md) ⟩ [methods](/web/appendix/gas/app/member.md) ⟩ sheetByName()

{% tabs %}
{% tab title="💾 程式" %}

* ⬆️ 需要： [app.sheet.prototype](/web/appendix/gas/app/prototypes/app.sheet.prototype.md)

```javascript
  // 🔸 app.sheetByName()
  sheetByName(name) {

    const ss = app.spreadsheet;
    let sheet = ss.getSheetByName(name);

    // if no such sheet, append a new one.
    if(sheet === null){ 
      let i = ss.getNumSheets();          // 目前的有幾頁
      sheet = ss.insertSheet(name, i);    // 插入最後面 (⭐️ will activate automatically)
    }

    sheet.activate();                     // ⭐️ activate before returning        
    sheet.__proto__ = app.sheetPrototype; // ⭐️ extend sheet 
    return sheet;
  },
```

{% endtab %}

{% tab title="📘 手冊" %}

* Spreadsheet ⟩
  * [getSheetByName()](https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet?hl=en#getsheetbynamename)
  * [getNumSheets()](https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet?hl=en#getnumsheets)
  * [insertSheet()](https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet?hl=en#insertsheet)
* Sheet ⟩
  * [activate()](https://developers.google.com/apps-script/reference/spreadsheet/sheet?hl=en#activate)
    {% endtab %}

{% tab title="🧨 雷區" %}
{% hint style="danger" %}
🧨 雷區：

* <mark style="color:blue;">`sheet.activate()`</mark> 回傳的物件竟然<mark style="color:red;">**不是**</mark>原來的 <mark style="color:blue;">`sheet`</mark>❗️❗️❗️
* 但兩者實際上還是指向同一個試算表頁面，怪❗️
  {% endhint %}

```javascript
sheet === sheet.activate()        // false ❗️❗️❗️ 
```

{% endtab %}
{% endtabs %}
