1
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?

More than 3 years have passed since last update.

GASでspreadsheetを操作するときのsetValueで注意する点

Posted at

自分が昨日ハマってひどい目にあったので、メモ代わりに。
昨日、自分の作ったTOCを作るGASを都合で拡張していた。それぞれのシートからコメントや修正した日を取得できると便利だなと思って、下のように修正した。

sample
    sheet.getRange(sRow,sCol,1,warray[0].length).setValue(warray);

ところが、これが動かない。出来上がる結果は

  • コンテンツ1 コンテンツ1 コンテンツ1

みたいな感じで、最初の配列がコピーされてsetValueされている。
全く理由がわからず、しばらく画面を眺めていて、はっと気がついた。
配列の中身を複数書きだすのはsetValueではなく、setValues だ!!!

sample
    sheet.getRange(sRow,sCol,1,warray[0].length).setValues(warray); // valuesでないとバグる

全くの下らないバグだったのだけど、書き込むlengthに1以上を与えてsetValueを実行したとき、コピーされるなど、マニュアルのどこにも書いておらず、キッカイな挙動をされたためにハマったわけだ。

同じことにハマる人がいないように、ここに残しておくのであったw

1
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
1
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?