VLookup not working on full numbered Value - google-apps-script

I created a Google Spreadsheet (File Name Product Test) and I have an ID field on column A which contains the word ID plus some letters and numbers (Example "ID-KNYT-12345"). The KNYT part is different per ID, some KNYT some DMXF etc.
So in column B I added a custom formula (Sample below) which processes the ID. If KNYT only numbers are kept. If DMXF the DMXF part is included plus the numbers.
I then have a vlookup/importrange formula on column C which is supposed to use the converted value in column B to lookup the value from another sheet and retrieve a certain information.
The problem is if the converted value contains all numbers like 12345 the vlookup fails, "Did not find value in lookup evaluation". If the converted value contains letters and numbers like DMXF-25452 the lookup works. If I manually type 12345 on column A the lookup works. Why would the lookup say it didn't find a result when the value is there? More details below
I checked, all cells involved are in format Number>"AUTOMATIC".
I checked, the value 12345 is definitely found on the other sheet (Imported Range)
I checked these values online, I found no hidden characters or spaces
The data is from an email with attached Excel file. I don't download the file, I just click to preview it and copy-paste the entire table over to my Product Test spreadsheet
The custom Formula:
function Convert(Thevalue)
{
Thevalue = Thevalue.toString().replace("ID-KNYT-", "");
Thevalue = Thevalue.toString().replace("ID-DMXF-", "DMXF-");
if (Thevalue == "DMXF-2245"){Thevalue = "Evaluated";}
if (Thevalue == "DMXF-3268"){Thevalue = "Pending";}
return Thevalue;
}
The Vlookup (Not actual sheet url just a sample)
VLOOKUP($B1,IMPORTRANGE("https://docs.google.com/spreadsheets/d/feiugsdfjhsdkjfhiesdfjh-p-dsflkjgsdf/edit#gid=000222333","sheet1!$A:$C"),3,FALSE)
UPDATE: This seem to fix it for me. Looks like if the return value is all numbers and no letters it is a NaN issue
if (!isNaN(Thevalue))
{
return Number(Thevalue);
}
else
{
return Thevalue;
}

Your custom function returns text strings. The vlookup() function does not consider the number 123 and the text string "123" equal. To make it work, convert the lookup keys to text with to_text(), like this:
=vlookup( to_text($B1), Sheet1!$A:$C, columns(Sheet1!$A:$C), false )
As RemcoE33 said, the custom function seems superfluous, because the same thing can be done with a plain vanilla spreadsheet formula that employs regexreplace(), like this:
=arrayformula( regexreplace( to_text(A2:A), "ID(?:-KNYT)?-", "" ) )

Related

How to combine these two App Script formulas?

If both columns E and K have value in them → then run the function in the column G.
As a regular Sheets formula, it looks like this (it works):
=if(not(isblank({$E12,$K12})),$K12*$E12," ")
I need App Script that does the same.
Why do I need a Script instead of regular formula? Because Arrayformula doesn't allow me to write in the same column, I will need this same script for other similarly made formulas.
1)This one is for checking if cells are blank (E12 and K12):
var isCellBlank = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Copy of Produkti').getRange().isBlank();
if(!isCellBlank) {
iSheet.getRange().setValues();
}
2)This one is for the function I want to run:
function FillFormulas() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Copy of Produkti');
var lastRow = spreadsheet.getLastRow();
spreadsheet.getRange("G4").setFormula("=if(not(isblank({$E12,$K12})),$K12*$E12," ")");
var fillDownRange = spreadsheet.getRange(4,7,(lastRow-1)); spreadsheet.getRange("G4").copyTo(fillDownRange);
}
Function onEdit(e) I have in a different file.
Can you help me fill in the first part of the Script (isCellBlank - get.Range i guess)?
And how to combine these two scripts, so that it does the same as regular sheet formula, just for the whole column (like Arrayformula)?
If you could add a hidden column before G you could follow this approach I've explained in this other post:
You could sort it out with hidden columns, and values that expand to the next column in which you'd like to have your result or input your value.
Instead a formula: =B3/B4 you'd have ={",",B3/B4}
So, if you manually insert a value in the right column, the formula in left column won't expand. As an example I've created a very dummy calculator of sides and hypothenuse. In B1 (column B in red would be hidden) I have:
={"","Res: "&(C3^2-C2^2)^(1/2)}
And so on in the next rows. In this picture I've inserted manual values in C1 and C2, so C3 has a result:
[]
1
And another example in another row:
You can test it out here and the extrapolate it to your formulas,
(I've erased the part with "Res. " so the result is an actual number, it was just for easier view in my examples):
https://docs.google.com/spreadsheets/d/1ZaEEGWtLU-LXBmg-xy80Ps_biuYhb8WxZBs0g1tGE00/edit?usp=drivesdk

Google Apps script "like" in if statement? Wildcards?

I have some scripting to format cell color. I would like to use LIKE to search inside the string (cell value). I am having a hard time finding something that works. I am very much new to Google Apps Script\Java.
Basically I want an if statement for a cell value, if it contains the word "extra" with anything before or after it. So it would match for 1extra and extra1, with any number of characters before or after.
Thanks for any help!
If you just want to check if a certain string is present, then you can use match
Script:
function myFunction() {
var sheet = SpreadsheetApp.getActiveSheet();
var A1_A6 = sheet.getRange("A1:A6").getValues();
// loop values of A1:A6
A1_A6.forEach(x => {
// since x is in [x] form, we get its first and only element
cell = x[0];
// if cell value has "extra" in it
// added i modifier since you want to match "123Extra123"
if(cell.match(/.*extra.*/i))
Logger.log(`"${cell}" matches.`);
});
}
Sample Data:
Output:
Note:
.* surrounding "extra" means that any number of any character (it will match any string, be it very long, or none at all)
modifier i after /.*extra.*/ means it will match regardless of the case (case insensitive - e.g. will match even if we want is "extra" and the one in the string is "Extra"). If you want to only match the exact string "extra", remove the i.
Reference:
match

How to get the value of the selected cells that you pass through arguments as numbers?

When Doing This Add Total Function I Made When It Adds The Selected Cells It Says That The Result Was Not A Number, But The Cells Selected Only Have Numbers.
function ADDTOTAL(ref1, ref2) {
var Number1 = ref1;
var Number2 = ref2;
var Number = Math.floor(Number1+Number2);
return Number;
}
It returns #NUM! and says the result wasn't a number, I'm new to this and have no idea what it is.
Issue:
I managed to replicate your issue.
You are either passing strings instead of cell references:
or the values of the cells you use as references are strings. For example "3 "
is a string because it has an extra space but also 3 is formatted as string:
Either way you pass strings but the arguments need to be numbers since you are doing a mathematical operation.
To make sure that the value of a cell is a number, do =ISNUMBER(A1). If that returns TRUE it means that the value of A1 is a number. To convert a string number into an actual number select the cells and go to Format => Number => Number on the top menu.
Keep in mind that if you add two strings together like "3" and "1" you won't get 4 but 31. I deliberately changed the format to plain text. In this case again =isnumber(A1) would be FALSE:
Solutions:
Assuming that the cells are actual numbers, you have the following options:
You pass the cell references:
You pass number arguments:
Last but not least, you can use parseFloat to convert the input arguments into numbers:
function ADDTOTAL(ref1, ref2) {
var Number1 = parseFloat(ref1);
var Number2 = parseFloat(ref2);
var Number = Math.floor(Number1+Number2);
return Number;
}

Google Sheets Script .getValue() and .getDisplayValue() returning #REF For Some Reason

I had this function working before, but for some reason it is not working anymore.
This is a picture of the three pieces of data I am trying to collect. They have references to a couple other sheets in my file including some 'INDIRECT's. The following is the simple code I am trying to run to obtain the value of one of the pieces of data:
var ss = SpreadsheetApp.openById(logKey).getSheetByName("Investing");
var value = ss.getRange("G2").getValue();
var formula = ss.getRange("G2").getFormula();
Logger.log("Value: " + value + " Formula: " + formula);
... And this is the output:
[16-10-06 09:50:04:531 EDT] Value: #REF! Formula: =AD$2
As you can see from the output, it is successfully reading the cell from the spreadsheet, but it is not obtaining the value from the cell, just the formula. I don't understand why this is the case as in my sheet, the value it is trying to obtain is not '#REF!'.
As I said before, I had this general piece of code working before, but I must have changed something such that it broke what I am attempting to do.
Additional note: I am unsure if this code may be the culprit for some reason, even though it worked like this before. This is simply to convert columns numbers to row numbers form another sheet for easy copy and paste:
=INDEX(
INDIRECT(
CONCATENATE("QUOTES!",
REGEXEXTRACT(ADDRESS(ROW(), ((ROW()-3)*5)+4), "[A-Z]+"),
"4:",
REGEXEXTRACT(ADDRESS(ROW(), ((ROW()-3)*5)+4), "[A-Z]+")
)
),
COUNT(
INDIRECT(
CONCATENATE("QUOTES!",
REGEXEXTRACT(ADDRESS(ROW(), ((ROW()-3)*5)+4), "[A-Z]+"),
"4:",
REGEXEXTRACT(ADDRESS(ROW(), ((ROW()-3)*5)+4), "[A-Z]+")
)
)
)
)
Short answer
To avoid the #REF! error, the formula should not be placed on the second row. It could be placed on row 3 and below rows.
Explanation
ADDRESS(ROW(), ((ROW()-3)*5)+4) on any cell of row 2 returns the following error message:
Function ADDRESS parameter 2 value is -1. Valid values are between 1 and 18278 inclusive.

make a table of data in google docs if cells in spreadsheet contain data

I am trying to write a script which creates a table in a google docs file with the results after a user has submitted a form.
In the form the user can enter up to 16 invoices and 16 payments; however on my summary pdf I don't want to display a table full of empty cells if the user has only entered one invoice.
I tried to write an if statement to say "If "invoice number" cell is not empty, then create a table row, if not, do nothing", but it creates the rows anyway even when the cells are empty and I can't quite figure out why.
Anyone have any ideas? Here's the code:
var invoiceData = [['Invoice Number', 'Invoice Amount', 'Due Date of Invoice']];
table1 = copyBody.appendTable(invoiceData);
var tab1row1=table1.appendTableRow();
if (sheet3.getRange("A20").getValue() ==! 0);{
tab1row1.appendTableCell(sheet3.getRange("A3").getValue());
tab1row1.appendTableCell("£" + sheet3.getRange("B3").getValue());
tab1row1.appendTableCell(sheet3.getRange("C3").getValue());
And the same code repeated for rows up to row 16 (but with changed numbers obviuosly).
The cell A20 (which is the one specified within the IF statement) is a cell containing the formula =VALUE(A3).
Thank you in advance.
The not operator, which is an exclamation point, can't be after the equal signs.
function testAfunction() {
Logger.log(1===1); //true
Logger.log(1!==99); //true
Logger.log(1==!99); //false
};
Also, remove the extra semi-colon.
Should be:
if (sheet3.getRange("A20").getValue() !== 0) {