Googleのスプレッドシートから、ループ処理で値を取得して共有ドライブのフォルダにファイル出力する一連のプログラムです。
ループ処理でセルから値取得、共有ドライブ取得、ファイルの書き出し、がそれぞれ別々で検索ヒットしてしまいます。
ですが現場では、それらを繋げた一連の処理で記述することが多いのではないでしょうか?
コピペしてちょっと編集すれば使えるようにメモで残しておきます。
BigQuery用のDDL文作成で、カラム数が多いテーブルのcreate table文でdescriptionをコピペしていくのが面倒だったので、自動作成用のスクリプトを作成したのが、ことの経緯です。
function get_column_info() {// ファイル出力のフォルダIDconst folder_id = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";// アクティブのシートを取得var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();var sheet = spreadsheet.getActiveSheet();// 2行目から処理開始var row_idx = 2;var tmp_col_info = "";// カラム名が空になるまで取得while (sheet.getRange(row_idx,5).getValue() != "") {// 出力カラム行の最終文字制御if (row_idx != 2) {tmp_col_info = tmp_col_info + ",\n";}// 行情報取得var dataset_name = sheet.getRange(row_idx,2).getValue();var table_name = sheet.getRange(row_idx,4).getValue();var col_name = sheet.getRange(row_idx,5).getValue();var type_info = sheet.getRange(row_idx,6).getValue();var comment_info = sheet.getRange(row_idx,8).getValue();// テーブル名がある場合はcreate table文を追加if (table_name != "") {tmp_col_info = tmp_col_info + "CREATE TABLE `" + dataset_name + "." + table_name + "\n";tmp_col_info = tmp_col_info + "(\n";}// カラム情報取得tmp_col_info = tmp_col_info + " " + col_name + " ";// 型と説明取得if (type_info == "INTEGER") {tmp_col_info = tmp_col_info + "INT64 OPTIONS(description=\'" + comment_info + "\')";} else if (type_info == "ARRAY") {// TODO <STRUCT<の部分が実装できていないtmp_col_info = tmp_col_info + type_info + " OPTIONS(description=\'" + comment_info + "\')";} else {tmp_col_info = tmp_col_info + type_info + " OPTIONS(description=\'" + comment_info + "\')";}row_idx++;}// 最後の改行コードと締め括弧tmp_col_info = tmp_col_info + "\n";tmp_col_info = tmp_col_info + ")\n";// ファイル出力const contentType = 'text/plain';const charset = 'UTF-8';// 出力フォルダ指定const folder = DriveApp.getFolderById(folder_id);// Blob を作成するconst blob = Utilities.newBlob('', contentType, 'create_table.sql').setDataFromString(tmp_col_info, charset);// ファイルに保存folder.createFile(blob);// 確認MsgBrowser.msgBox(tmp_col_info);}