0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

GASのシート指定について

Posted at

GASは基本的にJavaScriptと同じ文法で書くことができますが、スプレッドシートに紐づく「コンテナバインドスクリプト」で作業を行う場合、使用するシートを指定する必要があります。今回は、その「シートの指定方法」についてアウトプットしていきます。

スプレットシートの構成

使用するシートを指定する上で、以下の構成を知っておく必要があります。
image.png

シートの指定(デフォルト)

function trst(){
 SpreadsheetApp.getActiveSpreadsheet().getSheetByName("シート1").getRange()
 }

・SpreadsheetApp.getActiveSpreadsheet():今開いているスプシの指定
・getSheetByName("シート1"):シート名の指定(今回はシート1を指定)
・getRange():セル指定

これがシートを指定する方法です。しかし、これを毎回やるのは中々にしんどい...
そんな時におすすめの方法が次です。

一工夫で簡略化

function trst(){
    let speadsheet = SpreadsheetApp.getActiveSpreadsheet();
    let sheet = speadsheet.getSheetByName("シート1");
    sheet.getRange("A1").setValue("aiueo")

このように変数で置いてあげることで、以降はsheet.getRange()のみでセルの指定が可能となります。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?