Clear a cell using a macro in a separate sheet - google-apps-script

I would like to create a macro in sheets that would open a different spreadsheet and clear data from certain cells. I can do it in the same spreadsheet, but haven't been able to figure out how to do it when I have spreadsheet 'A' open and want to delete information on spreadsheet 'B'

You can use either SpreadsheetApp.openById('{your_sheet_id}') (see documentation) or SpreadsheetApp.openByUrl('{your_sheet_url}') (see documentation).
Then you can chain with the usual getSheet(), getRange() and clear() or clearContent() methods, e.g.:
SpreadsheetApp.openById('xxxxxxxxx').getSheetByName('Sheet1').getRange('A4').clearContent();

Related

Can we call the function "Show formulas" in Google app script for Spreadsheet

I am currently working on a script for a google spreadsheet. This script compares two sheets together and applies changes based on the differences between these sheets. First, I take the data from the sheets and put them in an array then i compare them and apply the changes before pasting them on their original sheets.
My problem is that there are formulas that fill the some cells of my sheets. However, when I run my script, it only reads the data that appears in the cells and so the formulas are deleted once the script is finished. However I'd like to keep these formulas on my sheets ..
To solve the problem I "show formulas" (Ctrl + `) before launching the script, so the script reads the formulas instead of the cells' value.
I would like to call this function "Show Formulas" (Ctrl + `) from my google script, is it possible? If yes, how?
Thank you in advance,
Victor
Yes, it is possible.
For example, instead of getValues() you can use getFormulas(), and later on, when you paste the data into the destination sheet: setFormulas()
Another possibility is two copy-paste the data into a temporary sheet with copyTo(destination, copyPasteType, transposed) chosing the copyPasteType PASTE_FORMULA
You can also use the Advanced Sheets Service and retrieve and set values with the valueRenderOption FORMULA

how to protect certain range if cell equals some value

I'm looking a way to "conditionally protect" some of my sheet's ranges. I know it's not possible without a script... So I would need this:
On open, protect range (A6:A) from being modified if A2="C". Else if, leave unprotected.
This should be repeated on open in 10 sheets with given names (Com1, Com2,...,Com10)
Any help would be appreciated
Check out the Apps Script reference to access and modify protected ranges in sheets. You can build a Protection object which contains permissions for who can edit a given range.
Google Sheets has simple triggers including a function that will run onOpen() which you can put your code inside. The full Apps Script reference for extending Sheets provides a great resource for building functions to edit and manipulate structures and data of Spreadsheets.
You'll want to use .getRange('A6:A') and build a condition which calls the .protect() method if A2 = 'C'.
You can also you use SpreadsheetApp.getActiveSpreadsheet.getSheets() to get all the sheets from the spreadsheet and iterate through the returned array using forEach() to apply your protection to all sheets.

Hide formula bar in google sheets

I want to achieve the following:
I do not want other users of the sheet to access the formula bar for certain cells/sheets of the spreadsheet
I want to block them from accessing the script editor (do not want to show the code) used for this sheet
I do not want other users of the sheet to access the formula bar for certain cells/sheets of the spreadsheet
This is not possible.
You could publish your spreadsheet but this will not allow to edit any content, or to use Google Forms, but this will allow respondents only to submit data, not to view the result of calculations.
To securely hide the formulas they should be in another spreadsheet. You could use IMPORTRANGE or a script to import/export the calculations result. Bear in mind that IMPORTRANGE isn't recalculated immediately and that scripts could be slower than built-in functions.
An alternative is to create a web app that makes the calculations and call it from a custom function.
I want to block them from accessing the script editor (do not want to show the code) used for this sheet
It's not possible to block editors to access the Script editor. For details see
Scripts Bound to Google Sheets, Docs, or Forms. To prevent that viewers get access to the code, block the spreadsheet for making a copy. For details see Change your sharing settings
You can set permissions per cell:
- Click the cell
- Left-mouse click or Ctrl+click -> menu 'Protect range'
- Click 'Set Permissions' in sidebar
- Select Only You
Now the content is still visible.
There is a work-around for this, which is quite complicated.
- Create another sheet, called Formulas.
- Protect this sheet the same way above.
- In your main sheet you can refer to formulas in the other sheet: =Formulas!B1
- Now you can write the formula in this cell B1 in sheet Formulas
=Sheet1!B1+Sheet1!C1-Sheet1!D1 (where Sheet1 is the name of the first sheet)
- Now hide the Formulas sheet

Google script: unprotect a sheet through function when user has no right to do so

I have a spreadsheet with different sheets in Google sheet, 3 users can edit each one a sheet (protections are set, each user can edit only one sheet). They all can execute a google script function that writes what they edited in a summary sheet. I don't want anyone to be abble to edit the summary sheet, so I set myself as the only available editor.
So my problem is to authorize the 3 users, only through the google script function, to write in the summary sheet. I tried to use the following function :
var unprotected = summarySheet.getRange('G3:G10');
protection.setUnprotectedRanges([unprotected]);
but since the users are not allowed to edit the summary sheet, and since the function is run with the active user, so they can't give themselves the right to unprotect a range in the summary sheet... Do you know how to workaround this problem?
Thanks a lot!
I see two script-based choices, one easy and one quite hard, and one sheet-based choice, that is easiest:
Easy:
You run the "summarize" script instead of them or, you set the summarize script run on a trigger out of your account. Then you actually leave protections alone. You could set the summarize script to run on open with error catching if the user doesn't have the necessary authority to unprotect the summary sheet and/or write to the summary sheet.
Hard:
When they run the "summarize" script it calls a published standalone script that has been given the authorization to make the necessary protection changes. I'll be honest, I wouldn't be able to code this but have seen/heard of similar implementations.
Easiest:
Finally, I want to make sure you've considered having the summary sheet itself contain the necessary formulas, parsing, etc. to summarize data from the other sheets without any need of scripts for this aspect of the sheet. The sheet could call custom functions as needed if the parsing or other summarization functionality is beyond built-in functions' capabilities. The sheet could stay fully protected and update itself in real time as users enter data (no need for users to trigger the summary creation, unless spreadsheet settings have auto-recalculate turned off).
Edited to add: put in A1 of Summary sheet something like:
=summarize()
And have that custom function return a 2-dimensional array of the summarized data.

When to use getActiveSpreadsheet?

I'm trying to understand when to use getActiveSpreadsheet().
I've seen numerous examples:
var ss = SpreadsheetApp.getActiveSheet();
But I've also seen:
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
Is the active spreadsheet implied in some circumstances?
Yes, it is implied. The reference for SpreadsheetApp states:
getActiveSheet()
Gets the active sheet in a spreadsheet. The active sheet in a
spreadsheet is the sheet that is being displayed in the spreadsheet
UI.
Including the getActiveSpreadsheet() is probably often done because the getting of a spreadsheet is needed for other sheets and methods and it keeps the command flow consistent when done everywhere.
getActiveSpreadsheet() is a method used by container-bound script.
A script is bound to a Google Sheets, Docs or Forms file if it was created from that document rather than as a standalone script. The file a bound script is attached to is referred to as a "container". Bound scripts generally behave like standalone scripts except that they do not appear in Google Drive, they cannot be detached from the file they are bound to, and they gain a few special privileges over the parent file.
getActiveSpreadsheet(), getActiveDocument(), and getActiveForm() allow bound scripts to refer to their parent file without referring to the file's ID.
In Google Sheets, getActiveSheet(), getActiveRange(), and getActiveCell() let the script determine the user's current sheet, selected range of cells, or selected individual cell. setActiveSheet(sheet) and setActiveRange(range) let the script change those selections.
Taken from https://developers.google.com/apps-script/guides/bound
getActiveSheet() is usefull when you are working with one sheet in the active spreadsheet.
You would use getActiveSpreadsheet() when you need to use other methods for example getSheets() would get all the sheets and you can access them as an array. Or if you wish to work with the spreadsheet as opposed to the sheet itself.
A good example would be the methods available to the spreadsheet class which would not be available for the sheet class