# SheetMethods

[GAS](/web/appendix/gas.md) ⟩ [app](/web/appendix/gas/app.md) ⟩ sheet

{% hint style="success" %}
利用 [mixin](/web/js/val/obj/extend/mixin.md) 的方式，讓個別的 Sheet 物件擁有方便的 methods：

```javascript
 Object.assign(sheet, SheetMethods);       // ⭐ extend `Sheet`
```

{% endhint %}

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

```javascript
/**
 * common methods for a sheet
 * - deleteExtraColumns()
 * - deleteExtraRows()
 * - writeObjects(obj, {row=1, col=1, clearSheet=false})
 * @mixin
 */
const SheetMethods = {

  /**
   * 🔸 delete extra columns in a sheet
   * @example
   * sheet.deleteExtraColumns()
   */
  deleteExtraColumns(){
    const j = this.getMaxColumns(); 
    const i = this.getLastColumn();
    if (j > i) this.deleteColumns(i+1, j-i);
  },

  /**
   * 🔸 delete extra rows in a sheet
   * @example
   * sheet.deleteExtraRows()
   */
  deleteExtraRows(){
    var j = this.getMaxRows(); 
    var i = this.getLastRow();
    if (j > i) this.deleteRows(i+1, j-i);
  },

  /**
   * 🔸 write objects' data into sheet
   * @param {[object]} objs - array of objects (all with same properties)
   * @param {number} row - first row number of the range where data is written into.
   * @param {number} col - first column number of the range
   * @param {boolean} clearSheet - decide whether or not to clear entire sheet first.
   */
  writeObjects(objs, {
    row   = 1, 
    col   = 1, 
    clearSheet = false
  }={}) {

    // get keys from first object
    let keys = Object.keys(objs[0]);

    // dimensions of the data range.
    var rows = objs.length;
    var cols = keys.length;
    
    // +1 row for the header row.
    let range = this.getRange(row, col, rows + 1, cols);

    // objects -> 2D array
    const values = [];
    values.push(keys);        // 1st row: header

    objs.forEach( obj => {
      const row = keys.map(key => obj[key]);   // obj --> [v1, v2, ...]
      values.push(row);
    }); 
    
    // clear sheet if necessary
    if (clearSheet) this.clear();

    // write 2D values into range
    range.setValues(values);
  },

};
```

{% endtab %}

{% tab title="🔴 主題" %}

* [sheet.values()](/web/appendix/gas/app/prototypes/app.sheet.prototype/sheetmethods/sheet.values.md)
  {% endtab %}
  {% endtabs %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://lochiwei.gitbook.io/web/appendix/gas/app/prototypes/app.sheet.prototype/sheetmethods.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
