Writing Logs in Google Addons - google-apps-script

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.

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.

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 can I get a user to use script (bound to a Gsheet) to update cell in another Gsheet (user do not have edit access)?

I have a Master Sheet with only me having edit access to it. I have another sheet (user sheet) where users can add some data that I want them to update my Master sheet only via a script (in the user sheet). Is this possible?
I already have the script that can copy data from the user sheet and knows where to paste it in the Master Sheet.
I could do this easily using VBA in excel but I am very new to Gsheet and using Gsheet scripts. Hope someone can help me please.
You cannot have the user's bound script directly access a Sheet they do not have edit access to. A bound script always runs runs with the permissions of the user at the keyboard.
One possible workaround:
You can create a Google Apps Script web app that has a doPost function that edits your master Sheet. The web app should be published so that it "runs as you", and has anonymous access (which also means you should be careful how to process the requests you receive). In the bound script, you can communicate to your web app through UrlFetchApp, which can send POST requests and give you a response.
The easier way to do this is to have them modify their sheets and then your master sheet reaches out and gets the data from them on a timed trigger. Although making your own webapp would work.

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

Identify the last edited sheet via google apps script

Is there a method or way of identifying the last edited sheet in a Google Spreadsheet using Google Apps Script?
You would have checked the official documentation and found that no such method exists.
To accomplish this, you would need to write a proactive script to:
Use an onEdit() trigger function to identify the occurrence and source of edits.
Record the details in a non-volatile store; perhaps another spreadsheet.
Respond to queries about the last edited sheet.