タグアーカイブ スプレッドシート、データベース

著者:admin

スプレッドシートをデータベースにして振替伝票

GoogleのスプレッドシートをデータベースにしてWEBページから振替伝票を送信すると、損益推移のグラフが更新されるようなものを作ってみました。

→こちら

ご自由に振替伝票を送信してみて下さい。


ちなみに、スプレットシートをデータベースとして利用する方法です。

① まず、googleドキュメントでスプレットシートを新規作成、シート名を”FORM_DATA”に変更する。
※下記scriptのFORM_DATAは自分の好きなシート名に変更できます。
(スプレッドシートの題名は何でも構いません。)
② ツール → Script Editorを開け、下記スクリプトをコピペする


/*
Copyright 2011 Martin Hawksey

Licensed under the Apache License, Version 2.0 (the “License”);
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an “AS IS” BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

/* Usage
1. Run the setup function (you’ll need to do this twice – 1st time to grant acces to Script Properties)
2. Share > Publish as service … set security level and enable service
3. Copy the service URL and post this in your form/script action
4. Insert column names on the DATA sheet matching the parameter names of the data you are passing
*/

function doPost(e) { // change to doPost(e) if you are recieving POST data
var ss = SpreadsheetApp.openById(ScriptProperties.getProperty(‘active’));
var sheet = ss.getSheetByName(“FORM_DATA”);
var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0]; //read headers
var nextRow = sheet.getLastRow(); // get next row
var cell = sheet.getRange(‘a1’);
var col = 0;
for (i in headers){ // loop through the headers and if a parameter name matches the header name insert the value
if (headers[i] == “Timestamp”){
val = new Date();
} else {
val = e.parameter[headers[i]];
}
cell.offset(nextRow, col).setValue(val);
col++;
}
//http://www.google.com/support/forum/p/apps-script/thread?tid=04d9d3d4922b8bfb&hl=en
var app = UiApp.createApplication(); // included this part for debugging so you can see what data is coming in
var panel = app.createVerticalPanel();
for( p in e.parameters){
panel.add(app.createLabel(p +” “+e.parameters[p]));
}
app.add(panel);
return app;
}
//http://www.google.sc/support/forum/p/apps-script/thread?tid=345591f349a25cb4&hl=en
function setUp() {
ScriptProperties.setProperty(‘active’, SpreadsheetApp.getActiveSpreadsheet().getId());
}


③ ペースト後プロジェクト名を決めて保存(プロジェクト名は何でも可)
④ 関数選択で「setUp」を選択し、実行、ポップアップした「Authorization required」にAuthorizeし、完了メッセージのページが表示される
⑤ もう一回「setUp」を実行、今回は何も表示しない
⑥ ファイル→バージョンを管理→新しいバージョンを保存
⑦ 「公開」→「WEBアプリケーションとして導入」、公開設定を選択、導入→URLをコピー(重要)
⑧ スプレットシートに戻って、A,B,C列の一行目にHTMLで記載するname属性で指定する名前を入力
⑨ スプレッドシートのファイル→共有→アクセスできるユーザー→WEB上で一般公開&アクセス設定を共同編集者に変更

 そして、最後にHTMLを作る。
→sample(ソースを表示して確認してください。)
 ※action部分に⑦でコピーしたURLを張り付ける。

form id=”formID” action=”⑦のURL” method=”post” target=”fake-target”

inputエレメントのname属性がそれぞれスプレットシートに対応しています。
送信したデータがスプレッドシートに反映されます。


→振替伝票を確認してください。