How to protect 1 column from editing? - google-apps-script

I have big spread sheet with multiple tabs and i am sharing it with my coworkers.
In column D on each tab I have same set of formulas.
Unfortunately my collages keep deleting them so I'm desperate to find the way to block it from editing. G sheets protection doesn't work as it disables filter function which is much needed.
I'm trying to block 1 column from editing in every tab on the spreadsheet.
I tried to use this but I don't understand why it isn't working really
function onEdit() {}
if(entryRange.getColumn() == 2) {
e.range.setValue((e.oldvalue == undefined? "": e.oldvalue))
}

Related

Add Checkbox in Sheets based on Criteria

I'm looking for a way to add a functional checkbox to a cell, based on the value of another cell. I assume it may need to be script, as everything I've found so far has stated checkboxes are input only, and cannot be created with formulas.
I'm looking to create an unchecked checkbox in column C if the value in column B is a certain value (="Tech Notes"). If the value in column B does not fall under the specific criteria, no checkbox should appear at all in column C.
I did find a thread where checkboxes would appear in all cells of a column, but would be checked/unchecked based on criteria, but I'm looking for the criteria to determine whether the checkbox appears at all.
Here's a sample sheet
Is this possible?
Thank you!
Solution:
Since a script is acceptable, you can make a simple trigger to create and remove checkboxes upon editing the cell:
function onEdit(e) {
if (e.range.getColumn() == 2) {
var sheet = e.source.getActiveSheet();
if (e.value === "Tech Notes" ||
e.value === "Intake Process")
sheet.getRange(e.range.getRow(),3).insertCheckboxes();
else
sheet.getRange(e.range.getRow(),3).removeCheckboxes();
}
}
You don't need to execute this function manually in Google Apps Script, it will trigger everytime you edit the sheet.
Sample Data:
References:
Insert Checkboxes
Simple Triggers

Problem, merging cell in a table document, google apps script [duplicate]

I've writen this function (thanks, #Mogsdad) to merge cells in a table in a text google document, like this:
function onOpen() {
// Add a menu with some items, some separators, and a sub-menu.
DocumentApp.getUi().createMenu('Sample')
.addItem('merge cells of a table', 'mergeCells')
.addToUi();
}
function mergeCells() {
var body = DocumentApp.getActiveDocument().getBody();
for (var p= 0; p< body.getNumChildren(); p++) {
var child = body.getChild(p);
if (child.getType() == DocumentApp.ElementType.TABLE){
// Assume we've already located our table
var table = child;
var tableRow = table.getChild(2); // gets third row
var tableCell = tableRow.getChild(1); // gets second cell in row
tableCell.merge(); // Merges seconde cell with first cell.
}
}
}
But when I run the code, I got this weird result (very different of the expected, with the merged cell with the same dimensions of the table):
Is there a way to fix it? (merged cell with the same dimensions)
[edit to address updates in Google Docs]
This currently not possible. You can know if a cell is merged by looking calling getColSpan and getRowSpan but there are no setter methods.
Please star the following issue to be notified by updates regarding this.
The merge function you found is not specific to table cells, it is there to merge any element with a previous sibling of the same type, joining their content.
[original answer]
If you were expecting to have a merged cell that, like what you can do in a spreadsheet, that is not possible. Simply because that's not possible in Google Documents (at least not yet). Therefore the API cannot do this (it can only do things that are also possible manually).
This merge function is not specific to table cells as you probably imagined. It is working as designed.
You can do this by a workaround. Add a drawing and add a table in this drawing document. In this document te option 'merge cell's' is a possibility if you select 2 cells and press the right mouse button. See this youtube video for a tutorial
Use the Advanced Documents Service, batchUpdate and mergeTableCells.
function mergeCells() {
const documentId = 'DOCUMENT_ID';
const resource = {
requests: [
{
"mergeTableCells": {
"tableRange": {
"tableCellLocation": {
"tableStartLocation": {
"index": 2
},
"rowIndex": 0,
"columnIndex": 0
},
"rowSpan": 1,
"columnSpan": 2
}
}
}
]
}
Docs.Documents.batchUpdate(resource, documentId);
}
Resources
https://developers.google.com/apps-script/guides/services/advanced#enabling_advanced_services
MergeTableCellsRequest
One workaround is to embed tables within tables instead of merging cells. That way you can still programmatically add or remove cells/rows without ruining the tables. If you set the cell padding to 0, any stray paragraphs to 1pt font and remove any cell borders and you can achieve almost the same effect without any merged cells.
For example, I've got a cell with 4 columns, where I want the last column to be merged as as a single cell. I also want to be able to add or remove rows with Apps Script.
Table embedded within a table:
This way I can add or remove rows from the embedded table on the left and the right cell will remain "merged". You will have trouble getting the cell borders to line up, but if you can do without them you can still get it looking nice, like this:
And without borders:
Here is how to solve the merged cell in tables of a MS document when converting to a Google document: The idea is to go back to the MS word document and remove the merged cells and then copy and paste it or convert MS word to Google document. In this way we can get easy conversion of tables in MS documents to Google documents. But, within Google documents the cell can't be merged.

Google Apps Script - Going up from bottom of sheet to an available value

I'm trying to do a macro in google sheets where I select a point in column N (in this case N100) and then simulate control up button to move up to the next available value in column N. But when I do this, I see the cell that's activated is the top of the column (looks like control up is pressed twice).
The code I have is below, any ideas on why this seems to be the case?
function ClearFormq() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Team1");
sheet.getRange("N100").activate()
sheet.getCurrentCell().getNextDataCell(SpreadsheetApp.Direction.UP).activate();
}
Thanks!

Increment Cell value by one by clicking

I want to write a formula in Google sheets so that I can attach to a button to make the value in a cell increase by 1 each time you click it.
Here is the best I could find on this topic.
I attempted to do this, but to no avail. I am using Windows 7 and Chrome.
First create a script to increment the cell value. For instance, to increment cell "A1" by one the following script would work:
function increment() {
SpreadsheetApp.getActiveSheet().getRange('A1').setValue(SpreadsheetApp.getActiveSheet().getRange('A1').getValue() + 1);
}
Save this script and open your spreadsheet and follow these steps:
Go to "Insert" > "Drawing" and draw a button using shapes and text.
Position your button accordingly, and right-click on it to display an
arrow on the side of it.
In the drop-down menu, select "Assign Script." and type the name of
your function. (In this case "increment")
The first time when you click on it, it'll ask for permission but
should work subsequently.
Update 2020
Motivation:
I would like to provide a different approach based on Marlon's comment:
what if we want to create multiple buttons to different rows and each
button modifies its own row? Is there a way not create this manually?
Solution:
You can use onSelectionChange to capture single click events and mimic the behaviour of a button.
The following script will increment the value of a cell in column A upon clicking on a cell in column B of the same row.
It also creates a "button" text on the fly but this is optional and up to the user.
To use this solution, simply copy & paste it to the script editor and save the changes. After that, it will automatically be used upon single click events. In this solution I used Sheet1 as the sheet name.
function onSelectionChange(e) {
const as = e.source.getActiveSheet();
const col = e.range.getColumn();
const row = e.range.getRow();
if (as.getName() == 'Sheet1' && col == 2){
const range = as.getRange(row,1);
range.setValue(range.getValue()+1);
e.range.setValue('Button');
}
}
Demonstration:
Restrictions:
While this approach works pretty well, there are two drawbacks:
There is a small delay between the clicks and the updates.
The trigger is activated when selecting a cell. If a cell is already selected, if you click it again the function won't trigger. You have to remove the current selection and then select it again.
Assign the following to a drawing inside your sheet and it will increase the value of the currently selected cell by 1:
function plusOne() {
var cell = SpreadsheetApp.getActiveSheet().getActiveCell();
var value = cell.getValue() * 1;
cell.setValue(value+1);
}

How to merge the two cells of a table in a Google text Document without this weird result?

I've writen this function (thanks, #Mogsdad) to merge cells in a table in a text google document, like this:
function onOpen() {
// Add a menu with some items, some separators, and a sub-menu.
DocumentApp.getUi().createMenu('Sample')
.addItem('merge cells of a table', 'mergeCells')
.addToUi();
}
function mergeCells() {
var body = DocumentApp.getActiveDocument().getBody();
for (var p= 0; p< body.getNumChildren(); p++) {
var child = body.getChild(p);
if (child.getType() == DocumentApp.ElementType.TABLE){
// Assume we've already located our table
var table = child;
var tableRow = table.getChild(2); // gets third row
var tableCell = tableRow.getChild(1); // gets second cell in row
tableCell.merge(); // Merges seconde cell with first cell.
}
}
}
But when I run the code, I got this weird result (very different of the expected, with the merged cell with the same dimensions of the table):
Is there a way to fix it? (merged cell with the same dimensions)
[edit to address updates in Google Docs]
This currently not possible. You can know if a cell is merged by looking calling getColSpan and getRowSpan but there are no setter methods.
Please star the following issue to be notified by updates regarding this.
The merge function you found is not specific to table cells, it is there to merge any element with a previous sibling of the same type, joining their content.
[original answer]
If you were expecting to have a merged cell that, like what you can do in a spreadsheet, that is not possible. Simply because that's not possible in Google Documents (at least not yet). Therefore the API cannot do this (it can only do things that are also possible manually).
This merge function is not specific to table cells as you probably imagined. It is working as designed.
You can do this by a workaround. Add a drawing and add a table in this drawing document. In this document te option 'merge cell's' is a possibility if you select 2 cells and press the right mouse button. See this youtube video for a tutorial
Use the Advanced Documents Service, batchUpdate and mergeTableCells.
function mergeCells() {
const documentId = 'DOCUMENT_ID';
const resource = {
requests: [
{
"mergeTableCells": {
"tableRange": {
"tableCellLocation": {
"tableStartLocation": {
"index": 2
},
"rowIndex": 0,
"columnIndex": 0
},
"rowSpan": 1,
"columnSpan": 2
}
}
}
]
}
Docs.Documents.batchUpdate(resource, documentId);
}
Resources
https://developers.google.com/apps-script/guides/services/advanced#enabling_advanced_services
MergeTableCellsRequest
One workaround is to embed tables within tables instead of merging cells. That way you can still programmatically add or remove cells/rows without ruining the tables. If you set the cell padding to 0, any stray paragraphs to 1pt font and remove any cell borders and you can achieve almost the same effect without any merged cells.
For example, I've got a cell with 4 columns, where I want the last column to be merged as as a single cell. I also want to be able to add or remove rows with Apps Script.
Table embedded within a table:
This way I can add or remove rows from the embedded table on the left and the right cell will remain "merged". You will have trouble getting the cell borders to line up, but if you can do without them you can still get it looking nice, like this:
And without borders:
Here is how to solve the merged cell in tables of a MS document when converting to a Google document: The idea is to go back to the MS word document and remove the merged cells and then copy and paste it or convert MS word to Google document. In this way we can get easy conversion of tables in MS documents to Google documents. But, within Google documents the cell can't be merged.