LANG SELRCT

Apps Script Reference  (Create: Create new Spreadsheet | Create new Apps Script

Sunday, August 16, 2020

スプレッドシートにsetValue()で配列を入れる時はtoString()してやる

toString()しないと先頭の要素しか入らなかった。



MISSION
スプレッドシートにsetValue()で配列を入れる


BEFORE 
toString()しないと先頭の要素しか入らない。

  const array = ["hello", "hi"];
  sheet.getRange("A1").setValue(array);



AFTER 
toString()すると要素たちが入る。

  const array = ["hello", "hi"].toString();
  sheet.getRange("A1").setValue(array);



KEY
配列をtoString()する。



コード.gs
function dummy() {// これはうまくいかない
  const url = "SPREADSHEET_URL";
  const sheet = SpreadsheetApp.openByUrl(url).getSheets()[0];
  const array = ["hello", "hi"];
  sheet.getRange("A1").setValue(array);
}


function SetValueToString() {// これでうまくいった
  const url = "SPREADSHEET_URL";
const sheet = SpreadsheetApp.openByUrl(url).getSheets()[0]; const array = ["hello", "hi"].toString(); sheet.getRange("A1").setValue(array); }


参考 

setValue(value)

Latest post

Extracting data from Google Sheets with regular expressions

Introduction Regular expressions are a powerful tool that can be used to extract data from text.  In Google Sheets, regular expressions ca...