Google Sheet If dropdown - google-sheets

I have drop down for column A where I want to select value from drop down only for certain times (ie. 3 times), after I select value from dropdown 3 times, if I try to select it 4th time, value should be removed from dropdown or not able to select. Is this possible using excel or google sheet?
https://docs.google.com/spreadsheets/d/1nbXAkK565V24KDTAzE68q8rQgQWzn-jDJz_6piNYyEw/edit?usp=sharing
In above google sheet, I had selected Red 3 times, now if I want to select Red 4th time, I should not be able to select or Red should be removed from list.
I know using excel VBA, I can do same using below code, but can we add same to google sheets?
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rngDV As Range
Dim oldVal As String
Dim newVal As String
Dim lVal As Long
Dim check2 As Long
If Target.Count > 2 Then GoTo exitHandler
On Error Resume Next
Set rngDV = Cells.SpecialCells(xlCellTypeAllValidation)
On Error GoTo exitHandler
If rngDV Is Nothing Then GoTo exitHandler
If Intersect(Target, rngDV) Is Nothing Then
'do nothing
Else
Application.EnableEvents = False
newVal = Target.Value
Application.Undo
oldVal = Target.Value
Target.Value = newVal
lVal = Application.WorksheetFunction _
.CountIf(Columns(Target.Column), _
"*" & newVal & "*")
If lVal > 3 Then
If newVal = "" Then
'do nothing
Else
MsgBox "Not available"
Target.Value = oldVal
End If
Else
If Target.Column >= 47 And Target.Column <= 56 Then
If oldVal = "" Then
'do nothing
Else
If newVal = "" Then
'do nothing
Else
Target.Value = oldVal _
& ", " & newVal
End If
End If
End If
End If
End If
exitHandler:
Application.EnableEvents = True
End Sub

So after doing some research I found the way of achieving what you were aiming for here.
Solution
In the class data validation it mentions a method to set the validation to null within the range class. You can check more details about this method here. In that method it mentions that if the parameter is set to null, it will disable the data validation (dropdowns) therefore not allowing to select other values.
Here is a piece of sample code self explained with comments to achieve what you want in your specific case:
function onEdit(){
// get the sheet and values to check if in that range there are more than 3 elements selected
var ss = SpreadsheetApp.getActiveSheet();
var values = ss.getRange('A1:A18').getValues();
// this variable is for counting the amount of elements selected
var count = 0;
for(i=0;i<values.flat().length;i++){
// if an element in that range is not empty
if(values.flat()[i]!=''){
count++;
}
}
// if the count is over 3 then disable the dropdowns
if(count>3){
ss.getRange('A1:A18').setDataValidation(null);
}
}
I hope this has helped you. Let me know if you need anything else or if you did not understood something. :)

Related

Processing each Row with pivot data from other cell

==================================
UPDATE 11 December 2019
My Question is more about Macro Script
The GOAL (in illustration)
to change below raw sheet:
to more readable format:
Basically what i'm doing is split the campaign name with the separator and parse it.
I don't have the problem if the function on only process single cell,for example:
on "Report" Sheet the CELL B2 , is taking data from "Data" B2 ONLY
i got problem when the return data require conditional operator that involve specific condition. So while processing cell B2, it require content from E2, D2, etc
=====================================
i'm taking data from Google Ads/Analytics API to Google Sheet on specific worksheet (i call it 'Raw Data').
Now i'm using pattern for the campaign, so i can easily split/break with separator in order for me to get specific data.
For Example:
With this, by using underscore as separator, i can split campaign name, into various data:
Campaign Objective: Sales
Campaign Title: TBMB
Network: SEM
Branch: All
Targeting: Keywords
..etc
Then i create new sheet called Called CReport which consist the same data from Raw Data sheet, but in much better visualization for marketing people.
Now, after searching on Google, i found the solution for self reference cell.
The script goes like this:
function getSegment(data,index){
temp=data.split("_");
return temp[index-1];
}
function dataParse(input,dataSegment){
return Array.isArray(input) ? input.map(function(e){
return e.map(function(f){
if(f!=""){
return getSegment(f,dataSegment);
}
}
)}
) : "false usage";
}
​So if i want to have a column with Network Name, i can place this formula on row 2 (because row 1 is for table header) something like this:
=ArrayFormula(dataParse('RAW DATA'!B2:B;2))
Now my question:
This works for self-reference cell, means if the data taken from B2 in RAW DATA sheet, it will be the only data referenced to cell in Campaign Report sheet.
If the pointer is in B2 on CReport Sheet require data not only from B2 in RAW DATA but also D2 Cell.
What script i need to add in my function ?
i'm expecting the chunk of code will something like this
function dataParse(input,dataSegment){
return Array.isArray(input) ? input.map(function(e){
return e.map(function(f){
if(f!=""){
segmentData=getSegment(f,dataSegment);
if(segmentData=="google"){
returnData=get reference from column D //<---
}else{
returnData=get reference from column E //<---
}
return returnData
}
}
)}
) : "false usage";
}
Hope its clear enough.
Thanks in Advance !
I modified your function in this way:
// range (String): It will be used to get the info in a range
function dataParse(input,dataSegment, range){
var val = "";
return Array.isArray(input) ? input.map(function(e, index){
return e.map(function(f){
if(f!=""){
// If col D has value google then take info from col B
if(f === "google") val = getDesiredRangeValue("B", range, index);
// else take info from col E
else val = getDesiredRangeValue("E", range, index);
// Take segment as needed
return getSegment(val,dataSegment);
}
}
)}
) : "false usage";
}
In order to make it work, I inserted an extra argument to the function. Now you will need to pass as an string the range in A1 notation in your ArrayFormula, this is because the input argument only gives you the values in the cells, and with that extra argument it will be possible to obtain extra info. To make it work fine, always use the same range as the next example shows:
=ArrayFormula(dataParse('RAW DATA'!D2:D5, 2,"D2:D5"))
or
=ArrayFormula(dataParse('RAW DATA'!D2:D, 2,"D2:D"))
Notice I also added a new function called getDesiredRangeValue, which will take the values from the column you need, depending if one of the cells from Col D has the value google. This is how the function looks:
/*
// A1 (String): The col from where you will want the info
// range (String): It will be used to get the info in a range
// index (Integer): It gives the index number from the main array gotten in the input arg
*/
function getDesiredRangeValue(A1, range, index){
var rowNumbers = range.match(/\d+/g);
// It checks if the range will has and end or it will prolong without specifying and end row
if(rowNumbers.length > 1){
var rangeCol = ss.getRange(A1 + rowNumbers[0] + ":" + A1 + rowNumbers[1]).getValues();
} else {
var rangeCol = ss.getRange(A1 + rowNumbers[0] + ":" + A1).getValues();
}
// It returns the whole value from each cell in the specified col
return rangeCol[index][0];
}
Code
Now your whole code will look like this:
// Global var
var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("RAW DATA");
function getSegment(data,index){
temp=data.split("_");
return temp[index-1];
}
/*
// A1 (String): The col from where you will want the info
// range (String): It will be used to get the info in a range
// index (Integer): It gives the index number from the main array gotten in the input arg
*/
function getDesiredRangeValue(A1, range, index){
var rowNumbers = range.match(/\d+/g);
// It checks if the range will has and end or it will prolong without specifying and end row
if(rowNumbers.length > 1){
var rangeCol = ss.getRange(A1 + rowNumbers[0] + ":" + A1 + rowNumbers[1]).getValues();
} else {
var rangeCol = ss.getRange(A1 + rowNumbers[0] + ":" + A1).getValues();
}
// It returns the whole value from each cell in the specified col
return rangeCol[index][0];
}
// range (String): It will be used to get the info in a range
function dataParse(input,dataSegment, range){
var val = "";
return Array.isArray(input) ? input.map(function(e, index){
return e.map(function(f){
if(f!=""){
// If col D has value google then take info from col B
if(f === "google") val = getDesiredRangeValue("B", range, index);
// else take info from col E
else val = getDesiredRangeValue("E", range, index);
// Take segment as needed
return getSegment(val,dataSegment);
}
}
)}
) : "false usage";
}
Docs
These are the docs I used to help you:
Class Sheet
Custom Functions

Sending emails to recipients if certain cell value is met by each receipient

Basically I've used Google Sheets to create an invoice tracker, and I want to send a reminder email to each of my clients when their invoice is due. I've already set the date and the count down, and now I want to send them the reminder email when the cell value reaches "2" meaning 32 days has passed since I've invoiced them.
I've gathered the codes from different sources online, and also I've set a 24 hr trigger to run the code once in a day. The email template is also in place. Data of each client (dates, names, addresses, etc.) are listed in separate rows.
My problem is that instead of sending 1 single email to the right client, the mailing app sends emails to all clients when any of them have a due invoice!
I'm not sure which function or code I should use.
I tried 'Email_Sent' thing, but couldn't get anywhere good with it!
function CheckMaturity() {
// Fetch invoice maturity
SpreadsheetApp.getActiveSpreadsheet().getSheetByName('InvoiceTracker').activate();
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
for (var i = 5;i<=10;i++){
var invoiceMaturityRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('InvoiceTracker').getRange(i, 13);
var invoiceMaturity = invoiceMaturityRange.getValue();
// Check invoice maturity
if (invoiceMaturity = 2){
// Fetch the email address
SpreadsheetApp.getActiveSpreadsheet().getSheetByName('InvoiceTracker').activate();
var templateText = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('EmailTemplate').getRange(1,1).getValue();
var currentAddress = ss.getRange(i, 15).getValue();
var currentInvoiceNo = ss.getRange(i, 3).getValue();
var currentInvoiceDate = ss.getRange(i, 4).getValue();
var currentClient = ss.getRange(i, 14).getValue();
var messageBody = templateText.replace('{client}',currentClient).replace('{invoiceNo}',currentInvoiceNo).replace('{invoiceDate}', currentInvoiceDate);
var subjectLine = 'Kind reminder - Invoice status';
MailApp.sendEmail(currentAddress, subjectLine, messageBody);{
SpreadsheetApp.getActiveSpreadsheet().toast('Invoice reminder sent to' +currentClient, 'Reminder sent', -1);
}
}
}
}
I want the app to send only one single email to the right (relevant) client.
I think you need the below. Please check the variables and references. The following code should be adjusted. The column 'A' should be replaced with the column in which you have the last record to prevent that you miss any clients. Furthermore, please check the comments in the code below.
.Range("A1047854").End(xlUp).Row
And hereby the full code:
Sub SendEmails()
Dim myOlApp As Outlook.Application, MailItem As Outlook.MailItem
Dim attachmentPath1 As String, attachmentPath2 As String
Set myOlApp = CreateObject("Outlook.Application")
'loop through a sheet (change index)
For i = 1 To ThisWorkbook.Sheets("index").Range("A1047854").End(xlUp).Row
'set key for check (or just do it directly in the if)
invoiceMaturity = ThisWorkbook.Sheets("index").Range("A" & i).Value
If invoiceMaturity = "2" Then
'you can load the variables first, before adding them to the email, or add them directly.
Name = ""
MailAddress = ""
Address = ""
currentInvoiceNo = ""
currentInvoiceDate = ""
currentClient = ""
'make item for each iteration (again)
Set MailItem = myOlApp.CreateItem(olMailItem)
'attachments
attachmentPath1 = "path/to/file.something" 'or set to ""(nothing)
'body
MailItem.HTMLBody = "<B>" & "<h3>" & "DRAFT:" & "</h3>" & "</B>" & "<br>" & _
"Dear, " & "<br>" & "<br>" & _
"Please find enclosed a kind reminder.." & "<br>" & "<br>" & _
"Please note, that.." & "</b>" & "<br>" & "<br>" & _
"Should you have any questions or comments on the above, please do let us know." & "<br>" & "<br>" & _
"Kind regards," & "<br>" & "<br>" & _
"Signature"
MailItem.to = MailAddress 'adjust email
MailItem.Subject = "[subject of email" & "a variable?" 'adjust subject
MailItem.Show 'or mailitem.send
'just to make sure
Set MailItem = ""
End If
Next i
End Sub

Google Script - Auto Fill Current Date-Time when select specific text in the drop down list

I wonder if anyone can help me with the following google script? I just begin to work on some google script and help my colleagues to track the production of his work.
Here is the field is explained below.
Column 9 = Status (Dropdown list: Pending, In-progress, Done)
Column 11 = Send Date
Column 13 = Start Date
Column 14 = End Date
How I want the script work
Example:
When Column 9 (dropdown list) value = "Pending" then auto fill the current
date to Column 11.
When Column 9 (dropdown list) value = "In-progress" then auto fill
the current date to Column 13.
When Column 9 (dropdown list) value = "End Date" then auto fill the current
date to Column 14.
The code I am working on to get when the status is "In-Progress".
I know I am wrong about this part. -> [s.getValue() !== 'In-progress']
But I am not sure where or how to put the certain text that I am looking for in here.
function onEdit(e) {
var s = e.source.getActiveSheet(),
cols = [9],
colStamp = 13,
ind = cols.indexOf(e.range.columnStart)
if (s.getName() !== 'Lab Cases Tracker' || s.getValue() !== 'In-progress' || ind == -1) return;
e.range.offset(0, parseInt(colStamp - cols[ind]))
.setValue(e.value ? new Date() : null);
}
Thank you so much!
This should do it:
function onEdit(e) {
var ss=SpreadsheetApp.getActiveSpreadsheet()
var s1=ss.getActiveSheet()
var s = e.source.getSheetName()
var col=e.range.getColumn()
var r=e.range.getRow()
var val=e.value
var curDate = Utilities.formatDate(new Date(), "GMT+1", "MM/dd/yyyy")
if(s=="Lab Cases Tracker" && col==9){
if(val=="Pending"){
s1.getRange(r,11,1,1).setValue(curDate);
}
else if(val=="In-progress"){
s1.getRange(r,13,1,1).setValue(curDate);
}
else if(val=="End Date"){
s1.getRange(r,14,1,1).setValue(curDate);
}
else{return}
}}

VB6 - How could I get the ListBox selected id

AS I using the VB6.0 for create a Dialog Box with the ListBox, but only I can get the String text with Trim(DlgText$("xxxxx")), for the other side still I could not found how to get it.
Most of the answer from network said that could be using [LisBox_ID].Selected to get the item that what they want, but I can not get the same result.
For my Code:
[Dialog]
Function aOpenDialog As Boolean
aOpenDialog = False
iArrayLoop = 0
Begin Dialog UserDialog ,,250,120,ScriptTitle,.ActivateDlgControls
Text 5,5,130,10,"Sub Booking End Date", .tf_InsertionSetEndDate
ListBox 5,15,100,100,aArrayList, .aArrayList
Text 110,5,130,10,"After Date [DD-MMM-YYYY]", .tf_AfterDate
TextBox 110,15,55,10, .txt_AfterDate
Text 110,25,55,10,"Change Reason", .tf_ChangeReason
TextBox 110,35,130,10, .txt_ChangeReason
OKButton 110,45,70,10, .btn_Save
CancelButton 110,55,70,10, .btn_Cancel
End Dialog
Dim dlg As UserDialog
aArrayList(1) = "Day1"
aArrayList(2) = "Day2"
Dialog dlg
End Function
[ActiveDlgControls]
Function ActivateDlgControls(ControlName$, Action%, SuppValue%)
If (Action% = 2 And ControlName$ = "btn_Save") Then
sMissingMessage = ""
If (Not IsDate(CStr(Trim(DlgText$("txt_AfterDate"))))) Then
sMissingMessage = sMissingMessage & "- Please input the correct day format"
Else
MsgBox Format(Trim(DlgText$("txt_AfterDate")), "dd mmm yyyy")
' This Area will be using for get the selected array item id
' I can found the selected items with String
MsgBox Trim(DlgText$("aArrayList"))
' Unknow way to found the selected items id
' MsgBox dlg.aArrayList.SelectedItem(x)
End If
If (sMissingMessage <> "") Then
ActivateDlgControls = 1
iCheckResult = 1
sMissingMessage = "Information Missing:" & sMissingMessage
MsgBox sMissingMessage
End If
ElseIf (Action% = 2 And ControlName$ = "btn_Cancel") Then
iCheckResult = 2
End If
End Function
Any idea how should I get the selected ListBox item?
I want to get the array number that I selected inside the LisBox.
Although I got an other stupid idea for get the index like as below code:
For iArrayLoopCheck = 0 To UBound(aArrayList)
If (aArrayList(iArrayLoopCheck) = Trim(DlgText$("aArrayList")))Then
MsgBox "You Select item: " & iArrayLoopCheck
Exit For
End If
Next
Still I was looking for any smart code/ items/ easy way to get the result quickly just like get the String value in array like: Trim(DlgText$("xxxxx"))
Best Regards,
KT
To get the selected index of the list box:
list.ListIndex
returns 0 if the first item is selected, 1 if the second item is selected, etc., and -1 if no item is selected.

How to email a row in google sheets

First post. I have just started to learn to code (2 days in) but this is way over my head for now.
I want to be able to email the contents of a row (all cells in that row) by right clicking the grey cell that highlights the entire row and then choosing an option in the drop down menu that either allows me to enter an email address or sends to a specific address that is entered into the code (I will always be sending to the same address).
In fact, choosing the option in the drop down menu isn't a necessity, just how I envisage it but any solution that allows me to bypass copying the row and pasting it into an email would work.
Any help would be greatly appreciated.
Cheers
Ok, so I know this is probably terrible coding but as I said, until a couple of days ago I had never even read a code, let alone tried to write anything. So please be nice!
So with some serious googling, copy/pasting, editing for my needs etc and using the very little I know, here is what I have come up with:
function onEdit(event){
//transfer a row once specific value entered into a cell within that row (without deleting original row)
// assumes source data in sheet named Needed
// target sheet of move to named Acquired
// test column with yes/no is col 4 or D
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
if(s.getName() == "Sheet1" && r.getColumn() == 5 && r.getValue() == "Email JM") {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Sheet2");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).copyTo(target);
}
};
function onEditEmail(){
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet2');
var startRow = 1; // First row of data to process
var numRows = 1; // Number of rows to process
// Fetch the range of cells
var dataRange = sheet.getRange(startRow, 1, numRows, 5)
// Fetch values for each row in the Range.
var data = dataRange.getValues();
for (var i = 0; i < data.length; ++i) {
var row = data[i];
var message = row[0] + ", " + row[1] + ", " + row[2] + ", " + row[3]; //whole row
var subject = "New Order";
if (row[0] != ""){
MailApp.sendEmail("example#email.com", subject, message);
//delete row straight after it is sent
sheet.deleteRows(1, 1);
}
}
}
Row needing to be emailed is transferred to a different blank sheet when I enter "Email JM" at the end of the row and by setting data validation for this column this can be done by clicking the drop down arrow (no typing). My onEditEmail trigger is set to every minute, which sends all the columns I need from "Sheet2) and then immediately deletes the row so it is not resent a minute later.
The only problem is if 2 orders are entered inside a minute, but this is a very small chance of happening (although I guess it will sooner or later).
Please point out where I can improve this.
Cheers

Resources