App Script: get certain spreadsheet by name instead of active spreadsheet - google-apps-script

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

Related

How to publish sheet (not spreadsheet) via 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.

Share a custom google sheet function with another email address without showing the source code

Iv'e written a custom google sheet function using Google Apps Script, and i would like to share it with another spreadsheet user, but i do not want to share the functions source code.
I've converted the Google Apps Script to a project, but now i have no idea how to link the project back to my sheet, so the function will work again.
I also do not want to publish the AddIn to the marketplace.
Test As Addin also doesn't work, the sheet is opened in a new tab, but the cells with the custom function says #NAME?.
What am i missing?
Explanation / issues:
The error #NAME? indicates that you are trying to use a function
that does not exist. As you also mentioned, this function does not
belong to the active spreadsheet but on a different project.
Unfortunately, it is not possible to share a custom function with other spreadsheets directly.
Possible Workarounds:
You can create an add-on.
Another workaround solution would be to create a library. That is a great alternative and the documentation is quite straightforward.
Related:
Creating add ons in Google Apps Script
How to call library function as custom function from spreadsheet formula?

How to create a new Spreadsheet with a connected script within an App Script?

Within a Standalone App ScriptI'm creating a new spread sheet using the following line:
var new_ss = SpreadsheetApp.create("File_Name");
Now I wanna link a predefined App Script to this spreadsheet. How to do this? I couldn't find any method within the Documentation or by searching Google
I found the ScriptApp, but it doesn't provide any methods. I also found the API, but i don't think its available within App Script
I was looking for the same answer and bacuase I haven't find it, I change my approach. I created a script bound to a Google Sheet. When a new Google Sheet is needed I make a copy of the "template" Google Sheet with the bound script. I have a sheet name Parameters so the users can control the bound script.

How to protect a range from creator / owner of Google Sheet using Google Apps Script?

I have developed a Google app script where I am generating new google sheet based on a template. After copying the Google sheet, we are changing the owner using setOwner method to specified email address. The requirement is to protect a range thus we use getRange method to select the range and then call protect() method and setDescription to show protect range description.
The issue is we don't want the person who run this script to have edit access. Because s/he is a creator, we tried to change the owner and used removeEditor as well, but it doesn't seem to work.
Thoughts: Is is possible to simuate the File copy run by another user such that the person who is running the script don't have access.
It's not possible to make a protection on such way that the spreadsheet owner will not be able to edit the protected sheet/range.
Bear in mind that only owners and editors are able to run scripts but they can't remove themselves as editors and and editor can't remove the owner.
From https://developers.google.com/apps-script/reference/spreadsheet/protection#removeeditoremailaddress
Neither the owner of the spreadsheet nor the current user can be removed

Writing Logs in Google Addons

I am writing a Google Spreadsheet add-on that will pull data from web pages (calling URLFetch with an hourly trigger) and write the data to a spreadsheet.
However, I only want this add-on to only write to the same spreadsheet even if the user is opening the add-on in a different sheet.
Is it possible to force the user to work with that add-on only in a particular sheet? TIA.
Yes it is possible, when calling the SpreadsheetApp service you can call the sheet you want by name or the one you need from the list of sheets.
SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];
SpreadsheetApp.getActiveSpreadsheet().getSheetName()
Then you would write the information in that sheet.
hope it helps.