2
2

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でRangeオブジェクトに2次元配列をsetValuesするとき(備忘録)

2
Posted at

困ったこと

ぼく「うーん、実行してみるまでspreadsheetに設定する値の数わからないんだよなー。」
ぼく「でもなんか、行ごとにインデックスをインクリメントして設定していくのもなー。」
ぼく「適当に2次元配列をRangeのsetValues()に渡したらよしなにしてくるだろー。」

let arr = [
  ['a', 'b', 'c'],
  ['d', 'e', 'f'],
  ['g', 'h', 'i'],
];
sheet.getRange(1, 1).setValues(arr);

もちろん怒られました。

値を設定したい2次元配列に対して、Rangeオブジェクトの行列が一致していないといけないみたい。

なんとかする

sheet.getRangeにシグネチャ違いがあって、列数と行数を設定できるようです。

sheet.getRange(1, 1, arr.length, arr[0].length).setValues(arr);
2
2
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
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?