关于sql server:Google Apps Script Get Textbox Value Insert into Database

Google Apps Script Get Textbox Value Insert into Database

有人可以帮我弄清楚如何获取文本框的值以写入 MS SQL 数据库吗?我的数据库连接工作正常,现在我只需要确定如何获取文本框的值并使用 Google Apps 脚本将其写入数据库。

现在下面的代码只会将单词 TEXT 插入到数据库中,而不是文本框的值。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
function doGet() {
  var app = UiApp.createApplication();

  var panel = app.createAbsolutePanel();
  var label = app.createLabel('Test Textbox');
  var text = app.createTextBox();
  var submit = app.createButton('Submit');

  panel.add(label);
  panel.add(text);
  panel.add(submit);

  app.add(panel);

  // Handlers
  var handler = app.createServerClickHandler('write');
  handler.addCallbackElement(panel);
  submit.addClickHandler(handler);

  return app;
}

// submit text to database
function write(e) {
  var app = UiApp.getActiveApplication();
  var text = e.parameter.text;

  var conn = Jdbc.getConnection("jdbc:sqlserver://IPAddress:1433;" +
  "databaseName=name;userusername;password=password;");

  var stmt = conn.createStatement();
  var sql ="INSERT INTO dbo.tbl_Student (studentFName) VALUES ('text')";
  var count = stmt.executeUpdate(sql,1)//pass in any integer to get auto incremented IDs back

  // check to see any auto generated keys come back. that is optional.
  var rs = stmt.getGeneratedKeys();
  while(rs.next()) {
    Logger.log(rs.getString(1));
  }

  rs.close();
  stmt.close();
  conn.close();    

  return app.close();
}

为了获取传入的文本框的值,需要为文本框创建一个名称:

修改var text = app.createTextBox();var text = app.createTextBox().setName('mytb');.

然后,在您的处理函数中,您可以通过 var text = e.parameter.mytb;.

检索它

另外,我认为您只是将文字单词 \\'text\\' 插入到您的数据库中。相反,请尝试使用如下文本变量创建字符串:

var sql ="INSERT INTO dbo.tbl_Student (studentFName) VALUES ("+text+")";


有效的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
function doGet() {
  var app = UiApp.createApplication();

  var panel = app.createAbsolutePanel();
  var label = app.createLabel('Test Textbox');
  var text = app.createTextBox().setName('mytb').setId('mytb');
  var submit = app.createButton('Submit');

  panel.add(label);
  panel.add(text);
  panel.add(submit);

  app.add(panel);

  // Handlers
  var handler = app.createServerClickHandler('write');
  handler.addCallbackElement(text);
  submit.addClickHandler(handler);

  return app;
}  

// submit text to database
function write(e) {
  var app = UiApp.getActiveApplication();
  var textbox = app.getElementById('mytb');
  var text = e.parameter.mytb;

  var conn = Jdbc.getConnection("jdbc:sqlserver://IPAddress;" +
  "databaseName=database;user=user;password=password;");

  var stmt = conn.createStatement();
  var sql ="INSERT INTO dbo.tbl_Student (studentFName) VALUES ('"+text+"')";
  var count = stmt.executeUpdate(sql,1)//pass in any integer to get auto incremented IDs back

  // check to see any auto generated keys come back. that is optional.
  var rs = stmt.getGeneratedKeys();
  while(rs.next()) {
    Logger.log(rs.getString(1));
  }

  rs.close();
  stmt.close();
  conn.close();

  return app.close();
}