-1

Using Google Apps Script, I know how to create a new sheet:

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

And I know how to publish this sheet by hand from the created sheet:

File ==> Publish to the web.

But how can I publish and get the URL for the published sheet I created without the point/click method above? I would like a piece of code that resembles:

var newSheet = SpreadsheetApp.create("newSheet");
var pubSheet = newSheet.publish(); 
var pubURL   = pubSheet.getURL();  
Rubén
  • 34,714
  • 9
  • 70
  • 166
  • 2
    Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. On topic and how to ask apply here. StackOverflow is not a design, coding, research, or tutorial service. https://stackoverflow.com/help/how-to-ask – NotZack Apr 10 '18 at 13:53
  • "I want code that does this" Ok? And? What is your specific question that you are having trouble with? For reference: https://developers.google.com/apps-script/reference/spreadsheet/ – tehhowch Apr 10 '18 at 13:54
  • Is it possible to publish a Google Spreadsheet using Google Apps Script without doing it by hand? – Preston Faulk Apr 10 '18 at 14:00
  • 1
    OP failed to actually do research: duplicate of (take your pick): https://stackoverflow.com/questions/38533957/google-sheets-api-how-to-publish-to-web-for-embeddable-sheet https://stackoverflow.com/questions/13016395/programmatically-get-a-spreadsheet-published-on-the-web https://stackoverflow.com/questions/40476324/how-to-publish-to-the-web-a-spreadsheet-using-drive-api-and-gas – tehhowch Apr 10 '18 at 15:33

2 Answers2

1

Edit:

As comment by tehhowch, your answer was already on SO. This answer is from STTP Smart Triathlon Training on SO question how to publish to the web a spreadsheet using drive API and GAS

You must first active Drive API from Console API (See Enable the Drive Platform) and activate it in your script (Resources > Google Advanced Services > Click on "Activate" on "Drive API" row )

//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); 
}

First answer:

As seen on the documentation page, you can use the File.setSharing(accessType, permissionType) method like :

// 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();

You can't just share one sheet by this method, but you can make something.

See also File.getFileById(id), SpreadsheetApp.getUrl(), SpreadsheetApp.getId()

Pierre-Marie Richard
  • 1,914
  • 2
  • 19
  • 25
  • Maybe I phrased my question incorrectly. I know how to change/set viewing/editing access, but essentially what I need is to "Publish to the Web". When the spreadsheet is open I can do this manually by selecting **file** then **publish to web** but I want to know how to do this in script instead. – Preston Faulk Apr 10 '18 at 15:04
0

Building off of Pierre's input to answer Preston's question about how to get the url of the newly published webpage:

The url will follow this format 'https://docs.google.com/spreadsheets/d/'+fileId+'/pubhtml. Thanks to @tanaike for this insight.

Here is how I took the code provided by Pierre and used it to publish the active sheet and then paste the published url in a free cell in another sheet.

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