我对谷歌应用脚本和融合表非常陌生。我正在为我的公司开发一个 CRM 应用程序,我正在使用谷歌网站、谷歌应用程序脚本和融合表来做到这一点。我还使用 O'Reilly 的“Google Script - Enterprise Application Essentials”一书中的一个示例来构建我的应用程序。我正在使用 Fusion 表作为我的项目的数据库,并运行函数 doOAuth() 以授予每个人对 fusion 表执行操作的权限。(请参阅下面的代码,其中包含我从书中获得的 doOAuth 代码)。该应用程序适用于我对融合表所做的每个命令。我可以读取表格的内容,也可以对其进行搜索,但是在写入新数据或更新表格上的现有数据时,我遇到了一个烦人的问题。我的应用程序上有一些文本框,我在其中插入了一些数据,并且这些数据被写入表中,到目前为止没有问题,当我向这些文本框添加常用文本时,我在更新数据或将数据插入融合表时没有问题。但是,当我向这些文本框中添加任何特殊字符时,例如:á、ã、à、ä、é、õ、í 等,我会收到一条消息,告诉我需要进行授权才能执行该过程。但是我已经授权了。好像授权丢失了。即使我再次运行 doOAuth() 函数,也不会授予权限。insertFusionObj() 和 writeFusionObj() 函数(在下面的代码中列出)在它们的参数是特殊字符时似乎有问题。我不知道这是功能问题还是融合表插入和更新问题。如果有人可以帮助我解决这个问题,我将不胜感激,
我期待着一些帮助。最好的问候拉斐尔·塞蒂·诺盖拉
代码:
/**
* ---FusionService---
*
* Copyright (c) 2011 James Ferreira
*
* 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.
*/
/**
* FusionService
* @author James Ferreira
* @documentation http://goo.gl/pBSvF
*
* @requires Script Insert ID: ?????
* ObjService http://goo.gl/JdEHW
*
* @requires The Fusion Table ID MUST be set in a Script property.
* This will allow you to change it at anytime using
* ScriptProperties.setProperty('FUSION_ID', '<your ID>');
*
*
* Search for records in a Fusion Table
*
* @params {string} target Quoted column name(s) ('First Name', 'Last Name') OR (*) for all
* @params {string} where valid statement see http://goo.gl/SkHI1
* 'Last Name' CONTAINS IGNORING CASE 'Ferr'
* AND 'First Name' CONTAINS IGNORING CASE 'J'"
*
* @returns {array} [[headers],[match row],[match row]...]
* If no match returns []
*/
function searchFusion(target, where){
var values = [];
if (target == '*'){
var headers = getFusionHeaders();
for(var i in headers){
values.push("'"+headers[i]+"'");
}
}else{
values.push(target);
}
var arrayResult = fusionRequest("get", "SELECT "+values.toString()+",ROWID FROM "+
FUSION_ID+" WHERE "+ where).split(/\n/);
for (var i in arrayResult){
arrayResult[i] = arrayResult[i].split(/,/);
}
arrayResult.splice(arrayResult.length-1, 1);
return arrayResult;
}
/**
* Updates a record in the Fusion table
* Note: fusionObj must contain rowid of Fusion table record
*
* @params {object} fusionObj object with properties from cameled column names
* @returns {string} OK if successful
*/
function writeFusionObj(fusionObj){
var values = [];
var headers = getFusionHeaders();
for(var i in headers){
if (fusionObj[camelString(headers[i])] != undefined)
values.push("'"+headers[i] +"'='"+fusionObj[camelString(headers[i])]+"'");
}
return fusionRequest("post", "UPDATE "+FUSION_ID+" SET "+values.toString()+
" WHERE ROWID = '"+fusionObj.rowid+"'")
}
/**
* Add a new record to a Fusion table
*
* @params {object} fusionObj object with properties from cameled column names
* @returns {integer} rowid useful for unique ID of record
*/
function insertFusionObj(fusionObj){
var values = [];
var columns =[];
var headers = getFusionHeaders();
for(var i in headers){
columns.push("'"+headers[i] +"'");
values.push("'"+fusionObj[camelString(headers[i])]+"'");
}
return parseInt(fusionRequest("post", "INSERT INTO "+FUSION_ID+
" ("+columns.toString()+") VALUES ("+values.toString()+")").substring(5));
}
/**
* Get the Fusion row ID for a given column header and unique value
*
* @returns {integer} Fusion ROWID for record
*/
function getFusionROWID(header, key){
return parseInt(fusionRequest("get", "SELECT ROWID FROM "+FUSION_ID+
" WHERE '"+header+"'='"+key+"'").substring(5));
}
/**
* get the column header names from Fusion Table
*
* @returns {array} [header, header, ...]
*/
function getFusionHeaders(){
var headers = [];
var result = fusionRequest("get", "DESCRIBE "+FUSION_ID).split(/\n/);
for (var i in result){
result[i] = result[i].split(/,/);
headers.push(result[i][1]);
}
headers.splice(0, 1);
headers.splice(headers.length-1, 1);
return headers;
}
/**
* Deletes a record in the Fusion Table
*
* @params {string} rowid the ID of a Fusion table row
*/
function deleteFusionRow(rowid){
fusionRequest("post", "DELETE FROM "+FUSION_ID+" WHERE ROWID = '"+rowid+"'");
}
/**
* The get or post request to the Fusion API
*
* @params {string} reqMethod get or post
* @params {string} sql String A sgl type request see http://goo.gl/aVP3B
* @returns {integer} Fusion rowid useful for unique ID of record
*/
function fusionRequest(method, sql) {
var url = "https://www.google.com/fusiontables/api/query";
if (USE_OAUTH){
var fetchArgs = googleOAuth_();
}else{
var fetchArgs = new Object();
fetchArgs.headers = {"Authorization": "GoogleLogin auth=" + getAuthToken_()};
}
fetchArgs.method = method;
if( method == 'get' ) {
url += '?sql='+sql;
fetchArgs.payload = null;
} else{
fetchArgs.payload = 'sql='+sql;
}
return UrlFetchApp.fetch(url, fetchArgs).getContentText();
}
/**
* @private for OAuth
*/
function googleOAuth_() {
var oAuthConfig = UrlFetchApp.addOAuthService('fusion');
oAuthConfig.setRequestTokenUrl("https://www.google.com/accounts/"+
"OAuthGetRequestToken?scope=https://www.google.com/fusiontables/api/query");
oAuthConfig.setAuthorizationUrl("https://www.google.com/accounts/OAuthAuthorizeToken");
oAuthConfig.setAccessTokenUrl("https://www.google.com/accounts/OAuthGetAccessToken");
oAuthConfig.setConsumerKey('anonymous');
oAuthConfig.setConsumerSecret('anonymous');
return {oAuthServiceName:'fusion', oAuthUseToken:"always"};
}
/**
* @private for client Auth
* @returns String(auth token for a user)
*/
function getAuthToken_() {
var response = UrlFetchApp.fetch("https://www.google.com/accounts/ClientLogin", {
method: "post",
payload: "accountType=GOOGLE" +
"&Email=" + ScriptProperties.getProperty('CUSTOMER_KEY') +
"&Passwd=" + encodeURIComponent(ScriptProperties.getProperty('CUSTOMER_SECRET'))+
"&service=fusiontables" +
"&Source=testing"
});
var responseStr = response.getContentText();
responseStr = responseStr.slice(responseStr.search("Auth=") + 5, responseStr.length);
responseStr = responseStr.replace(/\n/g, "");
return responseStr;
}
/**
* Used to authenticate to Fusion Tables
* Run it twice!
*/
function doOAuth(){
var method = 'get';
var sql = "SHOW TABLES";
Logger.log(fusionRequest(method,sql));
}
var USE_OAUTH = true;
var FUSION_ID = ScriptProperties.getProperty('FUSION_ID');