How to publish sheet (not spreadsheet) via Google Apps Script - google-apps-script

I'm trying to publish a sheet using Google Apps script and get its (published) URL.
The only solution I've found is this thread, however the proposed script publishes entire spreadsheet, not as suggested in the question, particular sheet. I'm not sure what syntax to use as fileId in
Drive.Revisions.update(resource, fileId, revisionId);
in order to publish only active sheet, not entire spreadsheet.

Unfortunately it is not possible to publish a single sheet with Google Apps Script/Drive API. This is a product limitation for Drive API, this request can be promoted for future development through here.
Also, you can't retrieve the URL from the published spreadsheet with the API but when the spreadsheet is published to the web, the URL follows a specific pattern, you can refer to this answer for more details.
The pattern to follow for a single sheet would be:
https://docs.google.com/spreadsheets/d/e/2PACX-SPREADSHEETID/pubhtml?gid=IDOFTHESHEET&single=true

You just can hide the other sheets in the spreadsheet
So if you publish only your desired one is shown.

Related

Can I link a script to a google sheets which already has a script?

I have a google sheets which is linked to a form and a google scripts that was created in the menu of google sheets. This script runs fine and I use it to format the data. However, I have another script which I created from the google developer console, which sends and receives data from a website. I need this second script to get the value of a cell in the google sheets.
How can I link this second form to the google sheets while keeping the script already linked to the sheets?
When looking at responses to other questions they seem to be about individual sheets in a set so just to clarify, when referring to google sheets I mean the whole google sheets document. I only have one sheet anyways.
If it helps: The script I want to add acts a bit like a server and is being deployed by google scripts while the script that is already linked is only run when I call it on the sheets.
There is not way of attaching a standalone script to a document making it a bound script (which is the actual terminology for the script linked the the document).
The simplest way would be to copy-paste the code to the other script. You can deploy the bounded script.
If you really need more than one project for whatever reason, you can enable the Apps Script API and use projects.create (read reference) to create another one. If you don't know what this means, you probably shouldn't use it as it's finicky at best.
I found a solution which was to link the script to the google sheets using
var ss = SpreadsheetApp.openById("SHEET_ID_HERE");
I can then use ss.getActiveSheet().getRange().getValue();
and other funtions.
Thanks for the help!

App Script: get certain spreadsheet by name instead of active spreadsheet

I have an App Scirpt that should work with certain Google Spreadsheet, let's say "database". I then embed this spreadsheet into other web documents, and schedule to run every minute.
From all the tutorials I learnt that I should use the getActiveSheet() function. But this deals with any opened active GSheet in the broswer.
How can I specify for the script to work exactly with the "database" Google Spreadsheet?
Checkout
SpreadsheetApp.openById(id) or
SpreadsheetApp.openByUrl(url)
and see if that helps you ?
Reference
If you want to use the name of the spreadsheet you'll have to use the DriveApp class:
DriveApp.getFilesByName(name)
Reference

How to get Google Apps Script project ID linked/bound to a Google Spreadsheet

I have a task to update Apps Scripts linked to some Spreadsheets using Google REST APIs. I know how to update script if I have script Id, but the problem is that I have only spreadsheet Id, and I need to updated linked/bound script.
I didn't find any method in Apps Script REST API that would allow listing scripts linked to a spreadsheet https://developers.google.com/apps-script/api/reference/rest/
I also tried Google Drive REST APIs https://developers.google.com/drive/api/v2/reference/children/list and https://developers.google.com/drive/api/v2/reference/files/list with no luck.
Currently, it is not possible to retrieve the bound script id from the parent spreadsheet id. You can star(star on top left) these issues to prioritize these issues:
https://issuetracker.google.com/issues/111149037
https://issuetracker.google.com/issues/117105106
Depending on your use case, There's a workaround mentioned by Tanaike here

publish a Google Spreadsheet through Google Apps Scripts

Is it possible to publish a Google Spreadsheet to the web using Google Apps Scripts? Right now I have to do it manually using File > Publish to the web....
I checked all of the Google Apps Scripts references and guides but don't see anything about publishing a script through GAS automation.
You want to achieve File > Publish to the web... using Google Apps Script.
If my understanding is correct, how about this sample script?
When you use this script, please enable Drive API at Advanced Google Services.
Sample script:
var spreadsheetId = "###"; // Please set this.
Drive.Revisions.update({published: true, publishedOutsideDomain: true, publishAuto: true}, spreadsheetId, 1);
Official document:
publishAuto: Whether subsequent revisions will be automatically republished. This is only populated and can only be modified for Google Docs.
published: Whether this revision is published. This is only populated and can only be modified for Google Docs.
publishedOutsideDomain: Whether this revision is published outside the domain. This is only populated and can only be modified for Google Docs.
Note:
Spreadsheet has the revision ID of 1 as the default, when new Spreadsheet is created. And by publishAuto, when the Spreadsheet is updated, the updated Spreadsheet is automatically reflected to the published Spreadsheet.
I used them to this sample script.
If you want to modify this settings, please modify the script.
References:
Advanced Google services
Revisions: update
If I misunderstood your question and this was not the result you want, I apologize.
Edit:
About the URL of published Spreadsheet, when the Spreadsheet is published manually, the URL can be retrieved like https://docs.google.com/spreadsheets/d/e/2PACX-###/pubhtml. In this case, 2PACX-### is not the Spreadsheet ID. Unfortunately, in the current stage, this URL cannot be retrieved by API. Drive API v2 had retrieved it with publishedLink before. But now, it cannot be retrieved. When Drive API is updated from v2 to v3, publishedLink was removed. This is the current situation.
But as a workaround, you can create the URL of published Spreadsheet using Spreadsheet ID. Please check the following URL.
https://docs.google.com/spreadsheet/pub?key=### spreadsheetId ###
You can access the published Spreadsheet using above URL.

Is there a way to make a google apps script always available? (google form/response sheet)

I have a google apps script that I use on the response sheet that results from a google form. I would like this script to always be available on any response sheet that gets created from a google form. I think I need to turn my script into an add-on, but I haven't found a simple explanation on how to do that.