對於使用JavaScript更新谷歌電子表格,你需要使用Drive REST API
以下是用於更新電子表格
function updateFile(fileId, fileMetadata, fileData, callback) {
const boundary = '-------314159265358979323846';
const delimiter = "\r\n--" + boundary + "\r\n";
const close_delim = "\r\n--" + boundary + "--";
var reader = new FileReader();
reader.readAsBinaryString(fileData);
reader.onload = function(e) {
var contentType = fileData.type || 'application/octet-stream';
var base64Data = btoa(reader.result);
var multipartRequestBody =
delimiter +
'Content-Type: application/json\r\n\r\n' +
JSON.stringify(fileMetadata) +
delimiter +
'Content-Type: ' + contentType + '\r\n' +
'Content-Transfer-Encoding: base64\r\n' +
'\r\n' +
base64Data +
close_delim;
var request = gapi.client.request({
'path': '/upload/drive/v3/files/' + fileId,
'method': 'PATCH',
'params': {
'uploadType': 'multipart',
'alt': 'json'
},
'headers': {
'Content-Type': 'multipart/mixed; boundary="' + boundary + '"'
},
'body': multipartRequestBody
});
if (!callback) {
callback = function(file) {
console.log(file)
};
}
request.execute(callback);
}
}
代碼可以使用下面的代碼來調用updateFile
功能
。
var blob = new Blob(['Cell Text 1,Cell Text 2'],{contentType:'text/plain'});
updateFile(SHEET_ID,'',blob,function(){
alert("Updated document");
})
相關問題在這裏:如何閱讀電子表格,使他們可以更新:http://stackoverflow.com/questions/37264159/h流至讀取用的電子表格- - 谷歌表-API的JavaScript – ghenne