LoginSignup
0
1

More than 5 years have passed since last update.

【GoogleAppsScript】二次元配列データを特定のスプレッドシートへ出力する

Posted at

作成目的

手順/方法

  1. 後述する関数 outputToSpreadsheet() へパラメータを設定し、呼び出す ※パラメータの詳細な説明は以下
    • target_spreadsheet_url → 出力先スプレッドシートのURL
    • start_column → 値を出力する開始列番号
    • num_of_row → 二次元配列データの行数
    • num_of_column → 二次元配列データの列数
    • two_dimensional_array_datas → 二次元配列データ

補足

具体例

  • パラメータが指定されたなかった場合は、ダミーデータが設定されるように対応した
output_to_spreadsheet.gs
/*
* 二次元配列データをスプレッドシートに出力する関数
* 
* @param {string } 取得するスプレッドシートURL
* @param {integer} 二次元配列データ値出力先 開始列番号
* @param {integer} 二次元配列データ値 行数
* @param {integer} 二次元配列データ値 列数
* @param {array  } 二次元配列データ
*
*/

function outputToSpreadsheet(target_spreadsheet_url, start_column, num_of_row, num_of_column, two_dimensional_array_datas) {

  // パラメータ target_spreadsheet_url 未指定の場合、本スクリプトを実装したスプレッドシートを対象とする
  if(!target_spreadsheet_url){
    var target_spreadsheet_object = SpreadsheetApp.getActiveSpreadsheet();
    var get_active_sheet_object   = target_spreadsheet_object.getActiveSheet();
    var active_sheet_lastrow      = get_active_sheet_object.getLastRow();
  } else {
    var target_spreadsheet_object = SpreadsheetApp.openByUrl(target_spreadsheet_url);
    var get_active_sheet_object   = target_spreadsheet_object.getActiveSheet();
    var active_sheet_lastrow      = get_active_sheet_object.getLastRow();
  }

  // パラメータ start_column 未指定の場合、 ダミーデータとして 1 を代入する
  if(!start_column){
    var start_column = 1;
  }

  // パラメータ num_of_row 未指定の場合、ダミーデータとして 列数を 1 にする
  if(!num_of_row){
    var num_of_row    = 1;
  }

  // パラメータ num_of_column 未指定の場合、ダミーデータとして 行数を 5 にする
  if(!num_of_column){
    var num_of_column = 5;
  } 

  // パラメータ spreadsheet_info_all 未指定の場合、ダミーデータとして、以下の二次元配列データを利用する
  if(!two_dimensional_array_datas){
    var two_dimensional_array_datas = [['a', 'b', 'c', 'd', 'e']];
  }

  get_active_sheet_object.getRange(active_sheet_lastrow+1, start_column, num_of_row, num_of_column).setValues(two_dimensional_array_datas);

}

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