2016-08-26 17 views
0

HTMLサービスを使用したgoogle appsスクリプトでは、基本的なユーザー情報を取得し、データをMySQLデータベースにロードするカスタムWebページインターフェイスを作成する方法。あなたが開始する前に、私はよくthis documentを読んで、以下のコードgoogle appsスクリプトhtmlサービスWebページ

function getHtml() { 
    var ss = SpreadsheetApp.getActiveSpreadsheet(), 
     html = HtmlService.createHtmlOutputFromFile('index'); 
    ss.show(html); 
} 


function getData(form){ 
    var firstName = form.firstName, 
     lastName = form.lastName, 
     dob = form.dob, 
     email = form.email, 
     phone = form.phone, 
     sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); 
    sheet.appendRow([firstName, lastName, dob, email, phone]); 
} 

//index.html 
<!DOCTYPE html> 
<html> 
    <b>Add Record</b><br /> 

    <form> 
    First name: <input id="firstname" name="firstName" type="text" /> <br /> 

    Last name: <input id="lastname" name="lastName" type="text" /> <br /> 

    DOB: <input id="dob" name="dob" type="date" /> <br /> 

    Email: <input id="email" name="email type="text" /> <br /> 

    Phone: <input id="phone" name="phone" type="text" /> <br /> 

    <input onclick="formSubmit()" type="button" value="Add Record" /> <br /> 

    <input onclick="google.script.host.close()" type="button" value="Exit" /> 
    </form> 
    <script type="text/javascript"> 
     function formSubmit() { 
      google.script.run.getValuesFromForm(document.forms[0]); 
     } 
    </script> 
</html> 
+0

はエラーがありますか?それとも問題? –

+0

私は問題があります。私はmysqlテーブルにレコードを追加する方法を知らない。 – user25407

答えて

1

を使用してGoogleスプレッドシートでこれをやっています。いくつかの範囲のIPアドレスをホワイトリストに登録する必要があります。

// Replace the variables in this block with real values. 
var address = 'database_IP_address'; 
var user = 'user_name'; 
var userPwd = 'user_password'; 
var db = 'database_name'; 
var dbUrl = 'jdbc:mysql://' + address + '/' + db; 

は今、あなたはこれがDB挿入機能である

function getData(form){ 
    var firstName = form.firstName, 
     lastName = form.lastName, 
     dob = form.dob, 
     email = form.email, 
     phone = form.phone, 
     sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); 
    sheet.appendRow([firstName, lastName, dob, email, phone]);// this will write data line on your spreadsheet 
    writeRecord(firstName, lastName, dob, email, phone)// this is where you call the database insert function 
} 

getData(form)関数からwriteRecord(...)機能の下に呼び出す必要があり、

function writeRecord(firstName, lastName, dob, email, phone) 
{ 
    var conn = Jdbc.getConnection(dbUrl, user, userPwd); 

    var stmt = conn.prepareStatement('INSERT INTO myTable' 
     + '(f-name,l_name,dob,email,phone) values (?,?,?,?,?,?)'); 
    stmt.setString(1, firstName); 
    stmt.setString(2, lastName); 
    stmt.setString(3, dob); 
    stmt.setString(4, email); 
    stmt.setString(5, phone); 
    stmt.execute(); 
} 
+0

getData()関数をどのように書き直すのですか?関数のgetData(形態){ VARのfirstName = form.firstName、 lastNameの= form.lastName、 DOB = form.dob、 メール= form.email、 電話= form.phone、 loadDataFromFormToTable = insertDataToTable(FirstNameとlastNameの、dob、email、phone); //フォームレコードをmysqlテーブルにロードするには? \t} – user25407

+0

私の答えを編集し、データベースの挿入関数を呼び出す 'getData(form)'関数を修正しました。 – iJay

関連する問題