新しいFusion Tableをコードで作成する例です
事前準備としてAPIを利用できるようにしておきます
今回は以下の内容を指定してテーブルを作成します
- テーブル名は "name": "TABLE NAME"で指定
- 列のヘッダをcolumnsの中で指定
- "name": "Text"
- "name": "Number"
- それぞれのデータの型(どんなデータを入れるか)を指定
- "type": "STRING"
- "type": "NUMBER"
作成したテーブルのURLをログに出します
その1
コード.gs
function create_table(){ var resource = { "kind": "fusiontables#table", "name": "TABLE NAME", "isExportable": false, "columns": [ { "kind": "fusiontables#column", "name": "Text", "type": "STRING" }, { "kind": "fusiontables#column", "name": "Number", "type": "NUMBER" } ] }; var table = FusionTables.Table.insert(resource); var BASE_URL = "https://fusiontables.google.com/data?docid="; Logger.log(BASE_URL + table.tableId); } |
設定できるプロパティ
Resource representations{ "kind": "fusiontables#column", "columnId": integer, "name": string, "type": string, "description": string, "graphPredicate": string, "formatPattern": string, "validateData": boolean, "validValues": [ string ], "baseColumn": { "tableIndex": integer, "columnId": integer }, "columnPropertiesJson": string, "columnJsonSchema": string } |
create_table()を実行すると以下のようなログが出力されます
そのURLを開くと
以下のように設定したテーブル名とカラム(列)名のテーブルが作成されています
このように列名と型をセットにした配列を用意して
var values = [
["ID", "NUMBER"],
["QUESTION", "STRING"],
["ANSWER", "STRING"],
["DATE", "STRING"]
];
このように設定したい
参考
Table: insert
https://developers.google.com/fusiontables/docs/v2/reference/table/insert
Column
https://developers.google.com/fusiontables/docs/v2/reference/column
Table / Resource representations
https://developers.google.com/fusiontables/docs/v2/reference/table#resource
About Fusion Tables
https://support.google.com/fusiontables/answer/2571232
こちらのQiita記事も参考にさせていただきました
https://qiita.com/U11/items/91e7da3884f2592d70e3
そのURLを開くと
以下のように設定したテーブル名とカラム(列)名のテーブルが作成されています
その2
入力する値を配列で用意して入力したい
このように列名と型をセットにした配列を用意して
var values = [
["ID", "NUMBER"],
["QUESTION", "STRING"],
["ANSWER", "STRING"],
["DATE", "STRING"]
];
このように設定したい
コード.gsfunction create_table(){ var values = [ ["ID", "NUMBER"], ["QUESTION", "STRING"], ["ANSWER", "STRING"], ["DATE", "STRING"] ]; var columns = []; for(var i = 0; i < values.length; i++){ var obj = {}; obj["name"] = values[i][0]; obj["type"] = values[i][1]; obj["kind"] = "fusiontables#column"; columns.push(obj); } var resource = { "kind": "fusiontables#table", "name": "TABLE NAME", "isExportable": false, "columns": columns }; var table = FusionTables.Table.insert(resource); var BASE_URL = "https://fusiontables.google.com/data?docid="; Logger.log(BASE_URL + table.tableId); } |
参考
Table: insert
https://developers.google.com/fusiontables/docs/v2/reference/table/insert
Column
https://developers.google.com/fusiontables/docs/v2/reference/column
Table / Resource representations
https://developers.google.com/fusiontables/docs/v2/reference/table#resource
About Fusion Tables
https://support.google.com/fusiontables/answer/2571232
こちらのQiita記事も参考にさせていただきました
https://qiita.com/U11/items/91e7da3884f2592d70e3