Google Script: Searching with a cell value - javascript

I want to add +1 to a value from a customer.
As example:
| G8 | K8 |
| Max Mustermann | 140 |
Goal:
| G8 | K8 |
| Max Mustermann | 141 |
I have a Script that works fine, but I would like to use the value (Max Mustermann) from a cell (G5).
function TextFinden() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet()
spreadsheet.getRange("G8:G" + spreadsheet.getLastRow()).createTextFinder("Max Mustermann").matchEntireCell(true).findNext().offset(0, 4).activate();
//value +1
var cell = SpreadsheetApp.getActiveSheet().getActiveCell();
var value = cell.getValue() * 1;
cell.setValue(value+1);
}
Goal example:
createTextFinder("G5")
So, I can write the Name in a cell (G5) and use a Button to add +1 to the Customer.
Any advice will be appreciated, thanks.

I believe your goal is as follows.
You want to retrieve a value from cell "G5".
You want to search the value from cells "G8:G".
When the value is found, you wan to add 1 to the column "K" of the same row.
In this case, how about the following sample script?
Sample script:
function myFunction() {
const sheet = SpreadsheetApp.getActiveSheet();
const value = sheet.getRange("G5").getDisplayValue();
const range = sheet.getRange("G8:G" + sheet.getLastRow()).createTextFinder(value).matchEntireCell(true).findNext();
if (!range) return;
const valueRange = range.offset(0, 4);
valueRange.setValue(valueRange.getValue() + 1);
}
Note:
If the value of column "K" is the string value, please modify valueRange.setValue(valueRange.getValue() + 1) to valueRange.setValue(Number(valueRange.getValue()) + 1)
References:
createTextFinder(findText) of Class Range
offset(rowOffset, columnOffset)

Related

var return to NaN instead of number

Hey so i've something like that
var zxc1 = 1846;
var zxc2 = 1649;
var zxc3 = 174;
var zxc4 = 27;
if(message.toLowerCase() == ('!asd')) { client.say(channel, `zxc1` +`(${+ zxc1.toLocaleString()})` +` | zxc2 `+`(${+ zxc2.toLocaleString()})` +` | zxc3 ` +`(${+ zxc3.toLocaleString()})` +` | zxc4 ` +`(${+ zxc4.toLocaleString()})` +` | :) `);}
After typing !asd:
Result : zxc1(NaN) | zxc2 (NaN) | zxc3 (174) | zxc4 (27) | 2
what's the problem?
Why is zxc3 and zxc4 working but zxc1 and zxc2 no
In ${+ zxc1.toLocaleString()} the + will be applying on the result of zxc1.toLocaleString() to coerce it back to a number (which seems counterproductive).
If you are not sure if something is a number I do recommend you use the number function e.g. ${Number(zxc1).toLocaleString()} rather than using +
The reason why 3 and 4 are working is because (most likely) your locale adds a thousands separator and so 1 and 2 are not castable back to numbers directly

how to show alert messege when date range overlaps existing date ranges in angularjs

i am working on a project where i have few existing dates
suppose Existing from dates and to dates are shown below
Existing From Date and to dates
27/11/2020 to 02/12/2020
01/01/2021 to 15/01/2021
and now if i am trying to enter a record with
24/11/2020 to 29/11/2020 or 29/11/2020 to 07/12/2020
i want to show alert date ranges should not overlap
i am not able to create logic which can fullfill all conditions
so basically
if there is an existing date range 5-10
Then 1-5 or 1-6 or 6-7 or 8-10 or 10-20 should not be allowed
here is my code i have written following lines where i am looing through existing records and comparing newly entering dates with existing records
angular.forEach($scope.Selling, function (index, value) {
if ((NewFromDate <= new Date(index.ExistingPackagePriceToDate) && NewToDate >= new Date(index.ExistingFromDate))) {
alert("dates should not overlap");
}
});
can anybody help me with this to build the logic
if it is in jquery still i can convert that.
Thanks in advance.
Ok, this is actually an interesting one. You need to figure out your cases of overlap
NewFromDate <= ExistingFromDate And NewToDate >= ExistingFromDate
Visually
| | -- Existing
| | -- New
Or
| | -- Existing
| | -- New
NewFromDate >= ExistingFromDate And NewFromDate <= ExistingPackagePriceToDate
| | -- Existing
| | -- New
Or
| | -- Existing
| | -- New
So your code would have to become something like this:
angular.forEach($scope.Selling, function (index, value) {
Console.log(NewFromDate.toString() + '-' + NewToDate.toString());
Console.log(index.ExistingFromDate + '-' + index.ExistingPackagePriceToDate);
if ((NewFromDate <= new Date(index.ExistingFromDate) && NewToDate >= new Date(index.ExistingFromDate))) {
alert("dates should not overlap");
}
if ((NewFromDate >= new Date(index.ExistingFromDate) && NewFromDate <= new Date(index.ExistingPackagePriceToDate))) {
alert("dates should not overlap");
}
});

Get First Row where ColA = Name, ColB = SecondName and ColC = null(is empty)

I'm very new to JavaScript so bear with me. I want an equation that searches multiple columns for specified values, not the same value across all columns. For example: ColA = James , ColB = Smith, ColE = ""(cellisempty) right?
Once it finds the first Row with that criteria, it gets that row to be used in another equation which would be setValue(ColE + gotRow, new Date()) I'm making a Time in/Time Out Sheet if you are curious. The Idea is the function searches for a person's name and for the next available space to place the time because the same person may have signed out and in multiple times on the same sheet. Once he has the row with an empty spot it places in the current time on that row, which makes it no longer empty, meaning next time the function runs, it will skip that row and go to the next one the guy signed out of.
here is a function I was given to try out my problem thanks to one of the Users.
function getValue(cellName) {
return SpreadsheetApp.getActiveSpreadsheet().getRange(cellName).getValue()
}
function setValue(cellName, value) {
SpreadsheetApp.getActiveSpreadsheet().getRange(cellName).setValue(value);
}
function rowWhereTwoColumnsEqual(value1,col1,value2,col2) {
var value1=value1;//testing
var value2=value2;
var col1=col1;//testing
var col2=col2;//testing
var ss=SpreadsheetApp.getActive();
var sh=ss.getActiveSheet();
var rg=sh.getDataRange();
var vA=rg.getValues();
var rA=[];
for(var i=0;i<vA.length;i++) {
if(vA[i][col1-1]==value1 && vA[i][col2-1]==value2) {
rA.push(i+1);
}
}
Logger.log(rA.push(i+1))
SpreadsheetApp.getUi().alert(rA.join(','));
}
I created a function below it, just for inputting values to see how it works
function getrowWhereTwoColumnsEqual(){
rowWhereTwoColumnsEqual('James','Sheet1!A',null,'Sheet1!C')
Logger.log(rowWhereTwoColumnsEqual('Ta','Sheet1!A',null,'Sheet1!C'))
}
The sheet I made has 'James' in A1 while C1 is empty. It also has 5 other rows full of random names.
Column A | Column B | Column C
=================================
James | C |
Kyle | E |
Micheal | T |
Sarah | K |
Tray | F |
John | D |
So I was expecting to get a response "1" because row 1 should be true for both, but instead I saw "7". Probably because Row 7 is blank in all columns so all columns equal the same. So I must have entered the values wrong, or the equation needs an adjustment. Like I said, I'm very new.
Edit 5/9/19 20:06
Thank you again Cooper. Your function works pretty well.
function test() {
findRows(1,getValue('A1'),2,getValue('B1'),3,null,'Sheet1')
Logger.log(findRows(1,getValue('A1'),2,getValue('B1'),3,null,'Sheet1'));
}
However, once I added values to C1, logger.log came up []
Column A | Column B | Column C
=================================
James | C | 5/9/19
Kyle | E |
James | T |
Sarah | K |
James | F |
John | D |
without the value, I received [1.0,3.0,5.0], but once I changed a value I received [] was expecting [3.0,5.0]
Edit 9:00 PST
I Changed the null to "" and it seems to be working.
Edit 9:25 PST
Final Equations function Test() is the one that was finished product and succeeded.
function getValue(cellName) {
return SpreadsheetApp.getActiveSpreadsheet().getRange(cellName).getValue()
}
function getValues(cellName) {
return SpreadsheetApp.getActiveSpreadsheet().getRange(cellName).getValues()
}
function setValue(cellName, value) {
SpreadsheetApp.getActiveSpreadsheet().getRange(cellName).setValue(value);
}
function findRows(c1,n1,c2,n2,c3,n3,name) {
var ss=SpreadsheetApp.getActive();
var sh=ss.getSheetByName(name);
var rg=sh.getDataRange();
var vA=rg.getValues();
var rA=[];
for(var i=0;i<vA.length;i++) {
if(vA[i][c1-1]==n1 && vA[i][c2-1]==n2 && vA[i][c3-1]==n3) {
rA.push(i+1);
}
}
return rA
}
function test() {
var passiveRow = findRows(1,getValue('A2'),2,getValue('B2'),3,"",'Sheet1');
Logger.log(passiveRow);
setValue('C' + passiveRow, new Date().toLocaleString());
}
In order for "" to work Row 1 must have a header. It Only works if there is only 1 result, which there should be in the end. it finds the name and a blank spot to put the date.
Try this:
c1,c2 and c3 are column number n1,n2 and n3 are values or strings or null and name is the sheetname.
function findRows(c1,n1,c2,n2,c3,n3,name) {
var ss=SpreadsheetApp.getActive();
var sh=ss.getSheetByName(name);
var rg=sh.getDataRange();
var vA=rg.getValues();
var rA=[];
for(var i=0;i<vA.length;i++) {
if(vA[i][c1-1]==n1 && vA[i][c2-1]==n2 && vA[i][c3-1]==n3) {
rA.push(i+1);
}
}
return rA
}
function test() {
Logger.log(findRows(1,10,2,2,3,null,'Sheet150'));
}

How Do I Use Both Math.max() and Math.min() to bound my dragger

I'm building a horizontally dragging page layout. There are 3 columns, with two dragging objects. Everything is working fine except setting a max/min value for the drag.
The layout looks like this, with the two dragging bars either side of colB.
---------------------------
colA | colB | colC
| |
| |
| |
| |
| |
| |
| |
| |
I need to set it so that colA's dragger's maximum width (which is its left value) is equal to colC's dragger. Likewise, ColC's minimum needs to be colA's draggers' left value.
The function looks like this:
function doDragA(e) {
dragA.style.left = (e.clientX) + 'px';
}
function doDragC(e) {
dragC.style.left = (e.clientX) + 'px';
}
and I'm trying to do something like this:
function doDragA(e) {
var posC = parseInt(document.defaultView.getComputedStyle(dragC).left, 10);
dragA.style.left = (math.max(posC), math.min(0)) e.clientX + 'px';
}
function doDragC(e) {
var posA = parseInt(document.defaultView.getComputedStyle(dragA).left, 10);
dragC.style.left = (math.max(0), math.min(posA)) e.clientX + 'px';
}
But i'm getting syntax errors. I've console logged the values and they come through, it's just the math max/min syntax I'm getting wrong.
Firstly javascript is case sensitive, and the math object is Math, not math.
Secondly, the usage of min and max is to supply a list of arguments, not just one - it returns the answer to 'what is the value of highest/lowest of these?'.
Assuming you want the value of e.clientX bounded by some values valueMax and valueMin:
var valueUpToMax = Math.min(valueMax, e.clientX);
var valueAtLeastMin = Math.max(valueMin, e.clientX);
// so we can combine those to bound on both sides:
var boundedValue = Math.max(valueMin, Math.min(valueMax, e.clientX));
It seems like they should be the other way around; we limit to a maximum value by using Math.min, but it's because we are limiting to a maximum not trying to get a maximally high number.
The general principle is to use functions like in math:
c = f(a, b);
e = g(c, d);
-> e = g( f(a, b), d);

The if statement doesnt work in my forloop

Currently I am working on Java script project.the project is for Checking the values selected from a UDDF file with the whole values in the UDDF file.But the if statement used for comparison is not working.I have put my code below
function parseUddf(){
var myArray=new Array("null","airportID","airportSiteNo","faaRgn","uddfVer","null","airportName","verificationDate",
"city","state","horizontalelevationdatum","horiAccuracy","elevAccuracy","orthoDatum","orthoAccuracy",
"magDec","verificationDate","airportOrthoElev","airportEllipsoidElev","airportElevationLoc",
"verificationDate","towerFloorOrthoElev","towerFloorEllipElev","verificationDate","arpLatitude","arpLongitude");
var selt=document.getElementById("textdis").value;
var x = document.getElementById("uddf").value;
x = x.split("#");
var x0 = x[0];
x0 = x0.split("|");
var j=x0.length;
alert(j);
for(var i=1;i<x0.length;i++){
alert("selt="+selt.toString());
if(selt.toString()==x0[i])
{
alert("success");
alert("i="+i);
alert(myArray[i]);
//document.getElementById("textdistitleid").value=myArray[i];
break;
}
}
}
the UDDF file values are:
|FAI |50219.A |AAL |1.07|
|FAIRBANKS INTERNATIONAL AIRPORT |1592011|
|FAIRBANKS |ALASKA |
|NAD83 |5 CM |15 CM |NAVD88 |25 CM |
|-20.4|1592011|
| 439.0| 470.0|20R+3990|1592011|
| 529.0| | |
| 644854.4|-1475123.2|
I need your help
Instead of using == you can use .equals():
selt.toString().trim().equals(x0[i].trim())
You can get your trim() function for javascript from here

Categories

Resources