Trying to understand how to set "if" criteria correctly. Below script works, but only if B55 is hardcoded with "yes" or "no" words. As soon as I use formula on gsheets to arrive to yes/no on B55 script doesn't recognize the results. What would be the fix there?
And another question while on the topic (if I may) what would be the correct formula to write if B55 > 0 then unhide (again assuming B55 is formulated to arrive to certain number). Thanks!
function onEdit(e) {
var activeSheet = e.source.getActiveSheet();
var range = e.range;
if (activeSheet.getName() !== 'Deal Inputs' || range.getA1Notation() == 'B55') {;
if (e.value === 'No') {
activeSheet.hideRows(56, 64);
} else if (e.value === 'Yes') {
activeSheet.showRows(56, 64);
}
}
An onEdit trigger always requires a user editing the spreadsheet. A change in the spreadsheet caused by the (re)calculation of a formula will not fire the onEdit trigger. See here
Related
Thanks in advance for your help! Our team has a Google Sheet that is used to manage projects.
There is a Start Date (Column E) and a **Due Date **(Column F) that can be set for any task, however some require it, some don't. For example, for call tasks, we put the same start/end date. We need both cells to be filled in so we can display a gantt chart with all activities.
Is there a way to automatically fill in the Start Date cell with the End date (and vice versa), if one of them is empty?
I didn't want to populate those cells with a formula to keep things simple an played around with the formulas in conditional formatting but couldn't find a way!
This code is not very elegant (I'm still pretty new), but this may work if you are just looking to add a start date where there is an end date and vice versa.
EDIT: Using your implementation plan, I was able to get a version of this script to work. There was an issue where the startDate and endDate arrays held time data (ex: 4/21/2022, 12:00 PM). I was able to work around this by splitting the element from the comma so that it would only show the date.
function FillDate() {
let sheet = SpreadsheetApp.openById('YOUR_SHEET_ID').getSheetByName('YOUR_SHEET_NAME');
let dateColumns = sheet.getRange(13,4,sheet.getLastRow(),2).getValues();
let startDate = dateColumns.map(function(r){return r[0].toLocaleString();});
let endDate = dateColumns.map(function(r){return r[1].toLocaleString();});
let headerRows = 13; //you may want to change this if you have more than one header row
for (i=0; i<sheet.getMaxRows(); i++)
{
if (((startDate[i] != '') && (endDate[i] != ''))||((startDate[i] === '') && (endDate[i] === '')))
{
//This is empty because you don't want to do anything if both Start and End Dates are empty or full.
}
else
{
if((startDate[i] == '') && (endDate[i] != ''))
{
let splitEndDate = endDate[i].split(",");
sheet.getRange(headerRows+i,4).setValue(splitEndDate[0]);
}
else
{
let splitStartDate = startDate[i].split(",");
sheet.getRange(headerRows+i,5).setValue(splitStartDate[0]);
}
}
}
}
In Google Sheet, I want to highlight only 2 columns out of 5 columns.
5 columns here but I want to highlight only 'Name' and 'Weight' columns if a cell contain the word 'Smith'
The outcome should be like this.
I want to input more name and if the name contain the word 'Smith', I want it to be automatically highlighted for name and weight columns.
I tried to use conditional formatting in Google sheet, and I could highlight only the name column.
This is what I tried.
Outcome was this.
You are not far, try the following formula in the conditional formatting:
=IF(REGEXMATCH($C3, "Smith"), 1, 0)
The formula given by #nabais works.
In conditional formatting though one does not need to use the starting IF function.
"Format cells if" is how conditional formatting rules are formed by default as noted in the official help page.
Create a rule.
Single color: Under "Format cells if," choose the condition that you want to trigger the rule. Under "Formatting style, choose what the
cell will look like when conditions are met.
Color scale: Under "Preview," select the color scale. Then, choose a minimum and maximum value, and an optional midpoint value. To choose
the value category, click the Down arrow Down Arrow.
So the following formula is all that is needed:
(Please adjust ranges to your needs)
=REGEXMATCH($G2, "Smith")
You can try the following code:
function highlight() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var data = ss.getRange('A1:E').getValues();
for (i = 1; i < data.length; i++) {
var tf = ss.getRange("B" + i).createTextFinder('smith');
tf.matchEntireCell(false);
tf.matchCase(false);
var result = tf.findNext();
if (result !== null) {
var range = result.getRow();
ss.getRange('B' + range).setBackground('Yellow');
ss.getRange('D' + range).setBackground('Yellow');
}
};
};
I wish to parse a series of documents in a Google Drive folder using regular expressions.
The documents contain equipment model and serial numbers. I wish to then copy the results to a google sheet row by row. I have managed a similar task with emails successfully but to no avail with google docs.
Can anyone offer some guidance. I have tested the regular expressions in the 'find and replace' menu in google docs and they work fine. The following is simply an attempt to see if I can capture some data and write it to a cell in the active sheet.
function write() {
var ss= SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var doc =
DocumentApp.openById('1ZNqJjSJo1wkD3eaCRTY64g98hYEY77D4MDU6XpvA4MI');
var body = doc.getBody();
var text = body.findText('(\W|^)GSS\d{2}H(\W|$)')
ss.getRange(1,1).setValue(text);
}
You want to retrieve all values matched by (\W|^)GSS\d{2}H(\W|$) in the document, and put the result to spreadsheet with row by row. If my understanding is correct, how about this modification? I think that there are several answers for your situation. So please think of this as one of them.
Modification points :
Retrieve text from document.
Retrieve all matched values using the regex.
For this situation, I used RegExp#exec.
Put the result to spreadsheet.
Modified script :
function write() {
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var doc = DocumentApp.openById('1ZNqJjSJo1wkD3eaCRTY64g98hYEY77D4MDU6XpvA4MI');
var body = doc.getBody();
// Modified script
var text = doc.getBody().getText();
var result = [];
var r = /(\W|^)GSS\d{2}H(\W|$)/g;
while ((res = r.exec(text)) !== null) { // or while (res = r.exec(text)) {
result.push([res[0]]);
}
ss.getRange(ss.getLastRow() + 1, 1, result.length, 1).setValues(result);
}
If this was not what you want, I'm sorry. At that time, could you please provide the sample input and output you need? I would like to modify my answer.
I have a google sheet that receives a list of phone numbers from an outside source. Phone numbers arrive in one of two formats:
Numbers that appear as 12345678901 are seen without error.
Numbers that appear as 1(234)567-8901 result in #ERROR!.
It seems that google sheets is reading the second set of numbers as a formula. When I click into an error cell, the phone number is preceded with "=+", as in "=+1(234)567-8901". I can fix this manually for the entire document by using Find and Replace with "Search within Formulas" checked.
Find: "=+"
Replace: " "
Is there any way to automate this within google apps scripts? I would like to run this function onEdit() so that #ERROR! phone numbers are fixed in real time.
You can remove the ()- characters using a spreadsheet formula, let's say the number was in cell A1, then in another cell you can put:
=CONCATENATE(SPLIT(A1, "()-" ))
which will remove the ()- characters.
If you would like to do this with a script then you can use replace to remove the ()-
.replace(/[()-]/gi, "")
apply above your number column range to properly format number.
EDIT
This should work, change "A1:A" to your column
function onEdit(){
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange("A1:A" + sheet.getLastRow());
var data = range.getValues();
var formulas = range.getFormulas();
for (var i=0;i< formulas.length;i++) {
if(typeof formulas[i] !== "undefined" && formulas[i] != ""){
formulas[i][0] = formulas[i][0].replace(/[=()+-]/gi, "");
data[i][0] = formulas[i][0].toString();
}
}
range.setValues(data).setNumberFormat("0");
}
guys!
I'm new to this website and also not good with coding. So I would really appreciate some help.
Right now I'm in need of a specific code to make a google sheet work perfectly.
To further explain:
I have a google sheet that a few information will be input by other co-workers. What I need is a code that will register the date in a specific cell and by whom the input was made on another cell.
So far this is what I have:
function onEdit(event) {
var sheet = event.source.getSheetByName("Input");
// Note: actRng = return the last cell of the row modified
var actRng = event.source.getActiveRange();
var index = actRng.getRowIndex();
var cindex = actRng.getColumnIndex();
// Note: date = return date
// Note: user = return the user email
var userCell = sheet.getRange(index,14);
var dateCell = sheet.getRange(index,2);
var inputdate = Utilities.formatDate(new Date(), "GMT+0200", "yyyy-MM-dd");
// Note(with hour): var inputdate = Utilities.formatDate(new Date(), "GMT+0200", "yy-MM-dd HH:mm");
//var user = event.user; // Note: event.user will not give you collaborator's Id
var user = Session.getEffectiveUser();
// Note: setValue = Insert in the cell the date when this row was modified
if (userCell.Value == null) {
userCell.setValue(user);
dateCell.setValue(inputdate)
}
}
My main problems/questions are:
I don't exactly need the last modifier, but the person who first input info on the cells. Therefore I tried that last IF (If the cell that is supposed to have the last modifier e-mail is blank, it means that nobody changed that row before, so the code should add the user on the userCell), although it is not working since every change I make it ignores the verification.
I also want to add that the event will only happen if you add values, if you delete them, nothing happens. (so far even when I delete cells, it counts as modification)
Most of the sheet is protected to avoid that people by accident erase some of the formulas, so the cells that this code changes are also protected. Is there a way to make the code bypass cell protection?
Please, help me identify what I'm doing wrong and hopefully I'll get this working perfectly! Thanks for the help !
If you want to prevent the script from firing when a cell is deleted, try:
var editedCell = SpreadsheetApp.getActiveSheet().getRange(e.range.getRow(), e.range.getColumn());
if (editedCell == "") {
return;
}
I would change Session.getEffectiveUser() to session.getActiveUser().
The last if statement is unnecessary. You want whoever most recently edited the field to be identified, along with the date.