此脚本擦除 Fusion Table 并使用电子表格中第一张工作表中的数据对其进行更新。您需要创建一个命名范围(称为“namedRange”,但您可以在代码中更改它)。命名范围上方的行应该是标题。您还需要设置要更新的 Fusion 表的 Table ID,并且需要确保表头名称与 Fusion Table 中的列名匹配。
该脚本有点粗糙,缺少注释,但希望它会对您有所帮助。
function onOpen() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var menuEntries = [ {name: "Update Fusion Table", functionName: "updateFT"} ];
ss.addMenu("Fusion Tables", menuEntries);
}
function updateFT() {
var tableID = '99999' // Add the table ID of the fusion table here
var email = UserProperties.getProperty('email');
var password = UserProperties.getProperty('password');
if (email === null || password === null) {
email = Browser.inputBox('Enter email');
password = Browser.inputBox('Enter password');
UserProperties.setProperty('email',email);
UserProperties.setProperty('password', password);
} else {
email = UserProperties.getProperty('email');
password = UserProperties.getProperty('password');
}
var authToken = getGAauthenticationToken(email,password);
deleteData(authToken, tableID);
var updateMsg = updateData(authToken, tableID);
var updatedRowsCount = updateMsg.split(/\n/).length - 2;
Browser.msgBox("Fusion Tables Update", "Updated " + updatedRowsCount + " rows in the Fusion Table", Browser.Buttons.OK);
}
function getGAauthenticationToken(email, password) {
password = encodeURIComponent(password);
var response = UrlFetchApp.fetch("https://www.google.com/accounts/ClientLogin", {
method: "post",
payload: "accountType=GOOGLE&Email=" + email + "&Passwd=" + password + "&service=fusiontables&Source=testing"
});
var responseStr = response.getContentText();
responseStr = responseStr.slice(responseStr.search("Auth=") + 5, responseStr.length);
responseStr = responseStr.replace(/\n/g, "");
return responseStr;
}
function queryFusionTables(authToken, query) {
var URL = "http://www.google.com/fusiontables/api/query";
var response = UrlFetchApp.fetch(URL, {
method: "post",
headers: {
"Authorization": "GoogleLogin auth=" + authToken,
},
payload: "sql=" + query
});
return response.getContentText();
}
function deleteData(authToken, tableID) {
var query = encodeURIComponent("DELETE FROM " + tableID);
return queryFusionTables(authToken, query);
}
function updateData(authToken, tableID) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var blockDataRange = ss.getRangeByName('namedRange');
var query = constructQuery(ss, blockDataRange, tableID);
// Browser.msgBox(query);
return queryFusionTables(authToken, query);
}
function constructQuery(ss, range, tableID, columnHeadersRowIndex) {
var sheet = ss.getSheets()[0];
var columnHeadersRowIndex = columnHeadersRowIndex || range.getRowIndex() - 1;
var numColumns = range.getEndColumn() - range.getColumn() + 1;
var headersRange = sheet.getRange(columnHeadersRowIndex, range.getColumn(), 1, numColumns);
var headers = headersRange.getValues()[0];
var data = range.getValues();
var queryPrepend = "INSERT INTO " + tableID + " ("+headers.join(",") + ") VALUES ('";
var query = "";
for (var i = 0; i < data.length; ++i) {
var hasData = false;
if (isCellEmpty(data[i][0])) {
continue;
}
query += queryPrepend + data[i].join("','") + "'); ";
}
return encodeURIComponent(query);
}
// Returns true if the cell where cellData was read from is empty.
// Arguments:
// - cellData: string
function isCellEmpty(cellData) {
return typeof(cellData) == "string" && cellData == "";
}