2017-09-29 56 views
0

我在我的谷歌應用腳​​本中使用文檔屬性來存儲mysql數據庫連接字符串並獲取錯誤TypeError:無法在第2行調用方法「setProperties」null的空白不知道什麼原因可能是。谷歌應用腳​​本方法getDocumentProperties返回null

dbmysqlconndev = PropertiesService.getDocumentProperties(); 
    dbmysqlconndev.setProperties({ 
    'mysqlUrl': 'jdbc:mysql://dbserverdev;databaseName=sample', 
    'mysqlUser': 'username', 
    'mysqlPassword': 'password'}); 
    var keys = dbmysqlconndev.getKeys(); 
    for (var i = 0; i < keys.length; i++) { 
     dbmysqlconndev.getProperty(keys[i]); 
    } 

var mysqlurl = dbmysqlconndev.getProperty(keys[0]); 
var mysqlusr = dbmysqlconndev.getProperty(keys[1]); 
var mysqlpwd = dbmysqlconndev.getProperty(keys[2]); 

回答

0

原因是,如錯誤消息所述,dbmysqlconndev爲空。現在,PropertiesService.getDocumentProperties()會返回null嗎?讓我們讀它的documentation

It is only available if the script is published and executing as an add-on or if it is bound to a Google file type. When document properties are not available this method returns null.

因此,這將是爲什麼:你可能執行此作爲一個獨立的腳本。使用getScriptProperties而不是getDocumentProperties

+0

我想爲我的MySQL數據庫連接字符串創建一個腳本庫,並從其他腳本調用函數DBConn()。 – user25407

+0

dbmysqlconndev = PropertiesService.getDocumentProperties(); dbmysqlconndev.setProperties('mysqlUrl':'jdbc:mysql:// dbtest; databaseName = test', 'mysqlUsr':'un', 'mysqlPwd':'pd'}); var keys = dbmysqlconndev.getKeys(); for(var i = 0; i user25407