如果我想发布Google Sheets电子表格,以便将其嵌入到iframe中的页面上,我将手动执行以下操作:
我如何通过使用前端的JavaScript通过Google编程实现上述目标?我正在我的应用程序中动态生成电子表格,并希望在创建后立即将它们嵌入到页面中。
一旦创建了工作表,我就可以动态地创建一个具有必要属性的iframe元素(工作表ID等)。这会造成一个错误。从这个question看来,表似乎需要有一个published: true属性,但这需要使用Drive -我正试图避免这种情况。这可以只通过Sheets来处理吗?
发布于 2016-07-27 15:14:49
在搜索了整个Sheets、googling和一般的灵魂搜索之后,我别无选择,只能包含这个驱动API并使用它来完成我的任务。这是我想出的解决办法。希望这能帮到别人!
在index.html文件中的客户端JS库中使用了Google的这个脚本:
<body>
...
<script type="text/javascript" src="https://apis.google.com/js/client.js"></script>
</body>那么对于JS的东西:
// Cache the api's into variables.
var sheets = gapi.client.sheets;
var drive = gapi.client.drive;
// 1. CREATE NEW SPREADSHEET
sheets.spreadsheets.create({
properties: {
title: 'new-sheet'
}
}).then(function(newSpreadSheet) {
var id = newSpreadSheet.result.spreadsheetId;
// 2. PUBLISH SPREADSHEAT VIA DRIVE API
drive.revisions.update({
fileId: id,
revisionId: 1
}, {
published: true, // <-- This is where the magic happens!
publishAuto: true
}).then(function() {
// 3. DISPLAY SPREADSHEET ON PAGE VIA IFRAME
var iframe = [
'<iframe ',
'src="https://docs.google.com/spreadsheets/d/',
id,
'/pubhtml?widget=true&headers=false&embedded=true"></iframe>'
].join('');
// We're using jQuery on the page, but you get the idea.
$('#container').html($(iframe));
});
});发布于 2016-07-25 19:19:48
正如您已经得出的结论,通过今天的Sheets API是不可能的,只能通过Drive (使用PATCH https://www.googleapis.com/drive/v3/files/fileId/revisions/revisionId请求,记录在https://developers.google.com/drive/v3/reference/revisions/update上)才有可能。
https://stackoverflow.com/questions/38533957
复制相似问题