Once the option in the dropdown is selected, that cell needs to be disable and no further changes it should permit.
Basically need to remove the edit rights for that particular cell once the option is selected or anything is entered in that dropdown cell(while dragging and filling the dropdown option).
SS Link: https://docs.google.com/spreadsheets/d/127P05KL1tzGru8PMXFLM57y20m1qOVnRS8IAfptsIh4/edit?usp=sharing
Since I am new to google script, I am sure it is possible but don't know the way out.
I believe your goal is as follows.
You want to protect the cell when the dropdown list of column "A" is changed.
In this case, how about the following sample script?
Sample script:
Please copy and paste the following script to the script editor of Spreadsheet and save the script. And, in order to run the script when the dropdown list of column "A" is changed, please install OnEdit trigger to the function installedOnEdit.
When you want to use this script, please change the dropdown list of the column "A". By this, the script is automatically run by the installable OnEdit trigger.
function installedOnEdit(e) {
const { range } = e;
const sheet = range.getSheet();
const dataValidation = range.getDataValidation();
if (sheet.getSheetName() != "Sheet1" || range.columnStart != 1 || !dataValidation || dataValidation.getCriteriaType() != SpreadsheetApp.DataValidationCriteria.VALUE_IN_LIST) return;
var protect = range.protect();
protect.addEditor(Session.getEffectiveUser());
protect.removeEditors(protect.getEditors());
if (protect.canDomainEdit()) {
protect.setDomainEdit(false);
}
}
When this script is run, the cell is protected. In this case, only the owner can edit the cell.
Note:
This script was tested using your provided Spreadsheet. When you change Spreadsheet, this script might not be able to be used. Please be careful about this.
References:
Installable Triggers
protect()
Related
I have a sheet that we use as a cash log which is updated daily.
I want to limit the amount of rows that are visible to the person entering data. To that end I added a column (#12/L in this case) which applies the number 1 to each new row of data, and sequentially increases the existing rows.
I have a filter applied to Column L that limits viewing to rows that are less than or equal to 5. This accomplishes what I want, but I have to click Filter/Ok to update the view when new rows are added.
Is there a way I could create an onEdit script to monitor Column L for changes, and reapply a filter when a change happens?
I believe your current situation and your goal as follows.
The Spreadsheet has the basic filter at the column "L".
When the value of the column "L" is updated, you want to refresh the filter. For this, you want to use OnEdit trigger. - This is from your question of Is there a way I could create an onEdit script to monitor Column L for changes, and reapply a filter when a change happens?.
In this case, how about copying the existing basic filter of column "L" and removing it, and creating the copied filter again? I thought that by this flow, your goal might be able to be achieved. When this flow is reflected to the Google Apps Script, it becomes as follows.
Sample script:
Please copy and paste the following script to the script editor of Google Spreadsheet. And, when you want to run this script, please manually edit the cell of the column "L" on the sheet of "Sheet1". By this, the script is run. The script copies the existing basic filter of the column "L" and remove it, and create it as new filter. By this, the filter can be reflected to the new value.
function onEdit(e) {
const sheetName = "Sheet1"; // Please set the sheet name.
const range = e.range;
const sheet = range.getSheet();
if (sheet.getSheetName() != sheetName || range.columnStart != 12) return;
const filter = sheet.getFilter();
const fRange = filter.getRange();
const criterion = filter.getColumnFilterCriteria(12).copy();
filter.remove();
fRange.createFilter().setColumnFilterCriteria(12, criterion);
}
Note:
This script is run by the OnEdit trigger. So when you directly run the script with the script editor, an error occurs. Please be careful this.
When the cell of the column "L" is updated by a script, in that case, I would like to recommend to include above method to the script. Because when the cell is updated by a script, the OnEdit trigger is not fired.
References:
Simple Triggers
Event Objects
Added:
From your following replying,
Thank you so much for your response. I'm definitely filing this one away for future use. I do realize that I was a little fast-and-loose with the word filter. Rather than a filter created as a result of a formula, I need to refresh a Filter View, i.e. one created by selecting the funnel and setting up a Filter by condition... An example of what I have can be viewed here: (docs.google.com/spreadsheets/d/…) Essentially nothing above the #5 (in Column L) should be visible after entering anything in Column A.
I understood that you wanted to refresh the basic filter of the column "L" when the column "A" is edited. From your question, I had thought that you wanted to refresh the basic filter of the column "L" when the column "L" is edited. But I understood that my understanding was not correct.
When you want to refresh the basic filter of the column "L" when the column "A" is edited, how about the following sample script?
function onEdit(e) {
const sheetName = "Sheet1"; // Please set the sheet name.
const range = e.range;
const sheet = range.getSheet();
if (sheet.getSheetName() != sheetName || range.columnStart != 1) return;
const filter = sheet.getFilter();
const fRange = filter.getRange();
const criterion = filter.getColumnFilterCriteria(12).copy();
filter.remove();
fRange.createFilter().setColumnFilterCriteria(12, criterion);
}
Is it possible for a Google Sheet dropdown, to auto select the first item in the list onChange / onEdit?
I'm unable to complete this directly on the sheet (I could be missing something), and I don't know enough about the Google Sheep App Script environment to know if it's possible.
I'm assuming that using Google App Script, this could be done.
I have setup this demo sheet, with everything working (other than what I'm after ;-)
https://docs.google.com/spreadsheets/d/1qzRT8rikVUfpZVOmugebB2yVHdLv_60K_Jwvxi6fX0U/edit?usp=sharing
What I would really like, When a "Tool" is changed/selected, I would like the "Colors" dropdown to select the first item in the list.
So if you select "Pencil", "Blue" would be auto selected into the cel. At the moment, the cel stays blank, until the user selects something.
Thank you.
I believe your goal as follows.
When the dropdown list of the cells "B5:B10" and the dropdown list of the column "C" is updated, you want to set the 2nd value of the updated dropdown list.
For example, when the dropdown list of the cells "B5" is set to "Pencil", you want to set the dropdown list of the cells "C5" to "Blue".
You want to achieve this using Google Apps Script.
In this case, I would like to run the script using the OnEdit trigger of the simple trigger. The sample script is as follows.
Sample script:
Please copy and paste the following script to the script editor of your sample Spreadsheet. And, please change the dropdown list of the column "B". By this, the script is run by the OnEdit trigger.
function onEdit(e) {
const range = e.range;
const sheet = range.getSheet();
if (sheet.getSheetName() != "Sheet1" || range.columnStart != 2 || range.rowStart < 5 || range.rowEnd > 10) return;
const r = range.offset(0, 1);
const value = [...new Set(r.getDataValidation().getCriteriaValues()[0].getValues().flat())].filter(e => e != "--")[0];
r.setValue(value);
}
In this sample, when the script is run, the range of the updated data validation rule is retrieved from the column "C", and retrieve the 2nd value of the dropdown list from the range.
Note:
In this sample script, from your sample Spreadsheet, when the dropdown list of cells "B5:B10" are changed, the script is run. So when you want to expand this range, please modify above script.
This sample script is for your sample Spreadsheet. When you change the sample Spreadsheet, the script might not be able to be used. So please be careful this.
References:
Simple Triggers
getDataValidation()
I have a simple onEdit function that I use on other sheets. I have had no issue when using this in the past. As of late, this function does not run.
function onEdit(e) {
var sheet = e.range.getSheet();
var name = sheet.getSheetName()
Logger.log(name)
if(e.range.getA1Notation() === "C3" && name == 'Main') {
sheet.getRange("C4").clearContent()
}
}
I even added the Logger.log() call to check and make sure I wasn't insane. But nope, it won't run.
I have even tried making a test sheet with no additional code to interfere with the onEdit call... no dice.
Any ideas? I'm pretty lost on this one.
Thanks,
Marcus
Based on the script you posted, the function would only be applicable to a sheet named "Main" and would only affect Cell C4 if Cell C3 is edited.
First of all, Logger.log would not show anything if the script was not initiated from the script code itself, therefore, it would not be possible to check that way. Therefore, I checked using var ui = SpreadsheetApp.getUi() and creating an alert. everytime i made an edit.
Secondly, based on that check, the script that you provided did indeed run. Therefore, this leaves 4 possible sources of error. 1) the sheet that you are making the edit on is not named "Main". 2)the cell you are editing is not cell C3, 3) the cell C4 is empty and there is nothing to clear 4) You did not make the edit, i.e. the change in C3 was not yet input into the cell because you have not keyed in enter or selected a place outside of the cell.
I have 2 CFs in a sheet that I would like to keep upon copy/pastes you can see the range and formulas below.
CF1 Range : B3:H1001
CF1 Formula : =($A3<>"")*(B3="") //This one higlights the empty cells in a given range
CF2 Range : A3:R1001 // This one highlights the row if the cell in R column is No.
CF2 Formula : =$R:$R="No"
Is there a way to protect these CFs or apply them again after copy paste ? Thanks !
When I copy paste from another range I am losing the Conditional formats that i have created. So I want them to stay like I formatted or I would like them to be applied again after the copy/paste.
From above replying, I could understand that you want to keep the conditional formats, which are =($A3<>"")*(B3="") for B3:H1001 and =$R:$R="No" for A3:R1001, even when the range is copied and pasted to B3:H1001 and A3:R1001.
If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.
Issue and workaround:
In the current stage, unfortunately, there are no methods for directly protecting the conditional formats in Google Apps Script. So in this case, as a workaround, I would like to propose to overwrite the conditional formats when the change of conditional formats and cells using OnChange event trigger.
Flow:
The range is copied and the parameters of conditional formats are changed.
The script is automatically run by the OnChange event trigger.
The existing conditional formats are deleted and new conditional formats are set.
Usage:
1. Copy and paste sample script
Please copy and paste the following sample script.
Sample script:
Please set the sheet name.
function onChange(e) {
const sheetName = "Sheet1"; // Please set the sheet name.
if (e.source.getActiveSheet().getSheetName() != sheetName || e.changeType != "OTHER") return;
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
var rules = sheet.clearConditionalFormatRules();
const rule1 = SpreadsheetApp.newConditionalFormatRule()
.setRanges([sheet.getRange("B3:H")])
.setBackground("green")
.whenFormulaSatisfied('=($A3<>"")*(B3="")').build();
const rule2 = SpreadsheetApp.newConditionalFormatRule()
.setRanges([sheet.getRange("A3:R")])
.setBackground("green")
.whenFormulaSatisfied('=$R:$R="No"').build();
sheet.setConditionalFormatRules([rule1, rule2]);
SpreadsheetApp.flush(); // This line might not be required.
}
In this sample script, 2 conditional formats in your question are set as the background color of green.
2. Install OnChange event trigger
Please install the OnChange event trigger to the function of onChange.
3. Test run
As a test run, please copy and paste the range in sheetName. By this, the script is run by the OnChange event trigger.
Note:
I think that OnEdit event trigger can be also used. But in this case, when the parameters of the conditional formats are changed, the event trigger is not run. So I used the OnChange event trigger.
References:
Installable Triggers
setConditionalFormatRules(rules)
Have successfully implemented OnEdit/OnChange triggers. However, these require an actual change to a cell.
It does not look like there is an analogous OnSelectionChanged trigger that fires when a new cell selection is made.
Any thoughts on how one might implement?
Good news. There is a new Simple Trigger, check this release note:
A new simple trigger, onSelectionChange(e), has been added for
Google Sheets. The onSelectionChange(e) trigger runs automatically
when a user changes the selection in a spreadsheet.
The following code illustrates how to use this trigger:
function onSelectionChange(e) {
var range = e.range;
if(range.getNumRows() === 1 && range.getNumColumns() === 1) {
SpreadsheetApp.getActiveSheet().clearFormats();
range.setFontColor("red").setFontWeight("bold")
}
}
If you add this function in your sheet bounded script the selected cell will always be highlighted as bold red. Please, note that this is just an example. The clearFormats() call will clear the format of all cells in the active sheet, so take care with it.