使用Google Apps脚本,我知道如何创建新工作表:
var newSheet = SpreadsheetApp.create("newSheet");
我知道如何从创建的工作表中手动发布此工作表:
文件==>发布到网上。
但是如何在没有上述点/点击方法的情况下发布并获取我创建的已发布工作表的URL?我想要一段类似的代码:
var newSheet = SpreadsheetApp.create("newSheet");
var pubSheet = newSheet.publish();
var pubURL = pubSheet.getURL();
答案 0 :(得分:0)
以Pierre的回答为基础,以回答Preston关于如何获取新发布的网页的url的问题:
URL将遵循以下格式:“ https://docs.google.com/spreadsheets/d/” + fileId +'/ pubhtml。感谢@tanaike提供this insight。
这是我如何使用Pierre提供的代码,并用它来发布活动工作表,然后将发布的url粘贴到另一个工作表中的空闲单元格中。
function getFirstEmptyRow() {
var spr = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Zapier");
var column = spr.getRange('A:A');
var values = column.getValues(); // get all data in one call
var ct = 0;
while ( values[ct][0] != "" ) {
ct++;
}
//Browser.msgBox(ct);
return (ct+1);
}
function publishToWeb(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var fileId = ss.getId();
var revisions = Drive.Revisions.list(fileId);
var items = revisions.items;
var revisionId =items[items.length-1].id;
var resource = Drive.Revisions.get(fileId, revisionId);
resource.published = true;
resource.publishAuto = true;
resource.publishedOutsideDomain = true;
Drive.Revisions.update(resource, fileId, revisionId);
var row = getFirstEmptyRow();
var zapier = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Zapier");
var url = zapier.getRange(row, 2);
url.setValue('https://docs.google.com/spreadsheets/d/'+fileId+'/pubhtml');
}
答案 1 :(得分:-1)
作为tehhowch的评论,你的回答已经在了。这个答案来自STTP Smart Triathlon Training关于问题how to publish to the web a spreadsheet using drive API and GAS
您必须首先从控制台API激活Drive API(请参阅Enable the Drive Platform)并在您的脚本中激活它(资源> Google高级服务>点击“驱动器API”行上的“激活”)
//this function publish to the web the document given by ID (google sheets or docs)
function publishToWeb(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var fileId = ss.getId();
var revisions = Drive.Revisions.list(fileId);
var items = revisions.items;
var revisionId =items[items.length-1].id;
var resource = Drive.Revisions.get(fileId, revisionId);
resource.published = true;
resource.publishAuto = true;
resource.publishedOutsideDomain = true;
Drive.Revisions.update(resource, fileId, revisionId);
}
如documentation page所示,您可以使用File.setSharing(accessType, permissionType)方法,例如:
// Get Spreadsheet and Id
var ss = SpreadsheetApp.getActiveSpreadsheet();
var id = ss.getId();
// Get the file object with the Spreadsheet Id
var file = File.getFileById(id);
// Set sharing parameters so ANYONE can VIEW this spreadsheet
file.setSharing(DriveApp.Access.ANYONE, DriveApp.Permission.VIEW);
// Get the Spreadsheet url, who is now accessible by anyone
var url = ss.getUrl();
你不能只用这种方法分享一张纸,但你可以做点什么。
另请参阅File.getFileById(id),SpreadsheetApp.getUrl(),SpreadsheetApp.getId()