【问题标题】:Create a Google Sheet then publishing it via Google Apps Script创建一个 Google 表格,然后通过 Google Apps 脚本发布它
【发布时间】:2018-04-10 13:51:11
【问题描述】:

使用 Google Apps 脚本,我知道如何创建新工作表:

var newSheet = SpreadsheetApp.create("newSheet");

而且我知道如何从创建的工作表中手动发布此工作表:

文件 ==> 发布到网络。

但是如果没有上面的指向/单击方法,我如何发布和获取我创建的已发布工作表的 URL?我想要一段类似的代码:

var newSheet = SpreadsheetApp.create("newSheet");
var pubSheet = newSheet.publish(); 
var pubURL   = pubSheet.getURL();  

【问题讨论】:

标签: google-apps-script google-sheets web-publishing


【解决方案1】:

编辑:

正如 tehhowch 的评论,您的答案已经在 SO 上。这个答案来自STTP Smart Triathlon Training关于SO问题how to publish to the web a spreadsheet using drive API and GAS

您必须首先从控制台 API 激活 Drive API(请参阅 Enable the Drive Platform)并在您的脚本中激活它(资源 > Google 高级服务 > 点击“Drive 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()

【讨论】:

  • 也许我的问题措辞不正确。我知道如何更改/设置查看/编辑访问权限,但基本上我需要的是“发布到网络”。当电子表格打开时,我可以通过选择 file 然后 publish to web 手动执行此操作,但我想知道如何在脚本中执行此操作。
【解决方案2】:

根据 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');
  
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-30
    • 1970-01-01
    • 1970-01-01
    • 2014-05-18
    • 1970-01-01
    • 2022-11-18
    相关资源
    最近更新 更多