How to get values from IXLCells (OpenXML) - closedxml

I want to find the cell in an Excel sheet that contains a certain text using closedXML. I use the following command:
IXLCells RefCells =PortfolioWorksheet.Search(strInternalReference, CompareOptions.None, false);
Accoridng to the Watch window in Visual Studio, RefCells does contain the correct cell address: C8 in my case.
I tried obtaining this value with:
RefCells.First().GetValue<string>()
Unfortunately I get the string I searched for and not the cell address.
Hope you can help!
Bas

GetValue() returns the content (value) of the cell. To get the address use Address.ToString() to get the full address or Address.RowNumber and Address.ColumnLetter (or Address.ColumnNumber) to get its parts.

Related

Search Name in 2 cells and return the value in third cell if the name matches

I would like to create a formula in my Google Sheet where I put the name of any person in one cell then the data related to that person/'s should populate against that cell (in a single cell only)
I'm attaching an image for reference
Request to share the possible solution for the above problem.
Thank you
I tried Vlookup but it is not showing up all the details and not working as well.
=TEXTJOIN(", ",TRUE,IF($E$2=$A$2:$A$12,$B$2:$B$12,""))
I tried the above Formula but it didn't work.
VLOOKUP just returns the first occurrence, altought you can use TEXTJOIN and FILTER to make that. Take a look at that:
=TEXTJOIN(", ";TRUE;FILTER(B2:B12;E2=A2:A12))
You can read more about that functions here:
https://support.google.com/docs/answer/3093197?hl=en
https://support.google.com/docs/answer/7013992?hl=en

Format a Google Sheets cell in numerical formatting 000 via Apps Script

I'm looking to set a column to format 000, which will display the zeros at begenning.
So, if a cell displays "3", I want that the script will set it to display "003".
This column is located in BDD tab, 13th column starting from the second row.
function FormattingGpeTrait() {
const sheet = SpreadsheetApp.getActiveSheet().getSheetByName("BDD").getRange(2,13)
sheet.setNumberFormat('000')
Modification points:
The method of "getSheetByName" is for Class Spreadsheet. In your showing script, you try to use it to Class Sheet. By this, an error occurs. This has already been mentioned in the comment. Ref
From 13th column starting from the second row., I thought that you might have wanted to set the number format of 000 to "M2:M". In your showing script, the number format is set to only a cell "M2".
If you want to set the number format to the cells "M2:M" of the sheet name of "BDD", how about the following modification?
Modified script:
function FormattingGpeTrait() {
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("BDD");
sheet.getRange("M2:M" + sheet.getLastRow()).setNumberFormat('000');
}
When you run this script, the number format of "000" is set to the cells "M2:M" of "BDD" sheet.
If you want to set the number format to the "M2:M", please modify getRange("M2:M" + sheet.getLastRow()) to getRange("M2:M").
References:
getActiveSpreadsheet()
getSheetByName(name)
The easiest way to get a range on a named sheet is to include the sheet name in the range reference, like this:
function formattingGpeTrait() {
SpreadsheetApp.getActive().getRange('BDD!M3:M').setNumberFormat('000');
}
I think that you can't use the standard number formats as they all will only evaluate your value to a real number value where '003' in reality is equal to '3' from a numeric sense.
You have two real options which is to either store the value in a Text column as "003" or prepend the value with an apostrophe "'003" which is basically the same as storing it as Text but the column can remain numeric.
You can create a custom number format for a cell/column to also do this but I am not certain how to accomplish this programatically. Basically, this is still going to end up like the Text variations I mention above, only you have a named format you can call. The data will still be stored as Text.

Google Sheets Macro won't copy/paste correct randomly generated value

I have a sheet with a formula that is randomly picking a name from a list. I have created a macro assigned to a button/image that will copy the randomly generated value into another cell, but the value that it copies is not the one it produces. For example, if the formula in A1 generates "John", I want to copy that value to B1, so that B1 contains "John", plain text no formula.
My problem is that the end result in B1 will not be "John", but rather a different name from the list (plain text no formula). My best guess is that running the macro makes the formula recalculate several times before actually copying what I want, so that what gets copied and pasted is not the desired result. How can I get the macro to correctly copy the value that it shows me before running it?
The macro attached to the image of the "Run" button I am using is:
var spreadsheet=SpreadsheetApp.getActive();
spreadsheet.getRange('A1').copyTo(spreadsheet.getRange('B1'),SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
The formula in A1 is:
=index(A3:A8,randbetween(1,6))
This produces one of the six names in A3:A8 at random.
In a successful result, while A1 contains "John", clicking the button would paste "John" into B1. A1 would then recalculate to a different name. If A1 contains "Steve", clicking the button would paste "Steve", and so on. Currently it will paste any of the names at random.
Here is a link to the sheet.
It's (probably) not possible to get RANDBETWEEN and macros to play nicely together, but one workaround is to use a macro to generate random numbers and paste them into your spreadsheet. You can then use that pasted number as the source for your random determinations, and it won't recalculate unless you run the macro again. That lets you use that particular random outcome in other macros. In my case, I used INDIRECT to let me use the random number as part of a cell reference, thereby picking a random name. Here's the macro I'm using for random numbers:
function getRandomInt(max) {
return Math.floor(Math.random() * Math.floor(max))+1;
};
With this function established, you can put it into other macros as getRandomInt(5), or whatever you want the maximum number to be. I added the "+1" to prevent zero as an outcome, but setting the maximum still works as normal.

Google Sheets formula that includes values from another cell

In Google Sheets, I have a formula in a cell that connects to an API with a script and spits out the JSON results. For example, I can connect to the Youtube API with a formula that looks like this,
=ImportJSON("https://www.googleapis.com/youtube/v3/videos?id=mv-cj6mBkPk&key=API KEY&fields=items(id,snippet(channelId,title,categoryId),statistics)&part=snippet,statistics")
I'd like to make part of that formula get it's value from a separate cell. For example, the video ID above (mv-cj6mBkPk), rather than have it typed in the above formula, I'd like to pull in the value from a cell (i.e. B1). Then I can create multiple versions of this formula with other video IDs (i.e B2, B3, etc.)
I've tried combining various parts of the formula through CONCATENATE-ing a few cells, but that doesn't seem to 'RUN' the formula, it just shows it.
My skills aren't so advanced in this area, so any help would be great. Thanks!!
It is not obvious what your problem is exactly, or where you got the code for the ImportJSON() function (as this is not a built-in function),
but a variation of the below SHOULD work - if it does not, then post what error you are getting (exactly).
Also, I presume you have substituted "API KEY" for the actual key... I have used API_KEY, so that the formula does not get broken across multiple lines here.
A1 : mv-cj6mBkPk
A2 : =ImportJSON("https://www.googleapis.com/youtube/v3/videos?id="&A1&"&key=API_KEY&fields=items(id,snippet(channelId,title,categoryId),statistics)&part=snippet,statistics")

How to display zero in place of error in filter formula in google spreadsheet

I am new to Google spreadsheet functions and trying to apply a formula in following way:
I have sheet1 & sheet2 in one workbook (name-formula4). I am doing sumproduct of filtered range from sheet1 to sheet2 with the following formula:
=SUMPRODUCT(filter(Sheet1!$A$1:$A$401, (Sheet1!$B$1:$B$401>= E1) * (Sheet1!$B$1:$B$401<= E2)))
Formula is perfectly ok when the range selected
(Sheet1!$B$1:$B$401>= E1) * (Sheet1!$B$1:$B$401<= E2)
has some value & is not empty. However if I leave the cells of said range empty (sheet1!b1:b3) -which are required to meet criteria- then this gives the error No Matches are found in filter evaluation. In a nut shell, I want to display 0 rather than displaying the error or NA.
Note that I cannot fill entire the selected B column since this receives live data from the form and not previous decided which one.
Please look at the following link for details and help in correcting the above formula:
https://docs.google.com/spreadsheets/d/1HTXf4VG2JupiP9UqCLRyAddYjhsom1leQOI9-DwfMaY/edit#gid=0
=IFERROR(SUMPRODUCT(filter(Sheet1!$A$1:$A$401, (Sheet1!$B$1:$B$401>= E1) * (Sheet1!$B$1:$B$401<= E2))),0)
I've had the same issue with my sheets too. I agree with the comments above hiding the error isn't best practice. However hiding the N/A response / customising it is very useful! You need to wrap the whole logic in a NA statement so it starts with it first. Here is how you can do something like this with the formula you are using.
=IFNA(SUMPRODUCT(FILTER((Sheet1!$A$1:$A$401,
(Sheet1!$B$1:$B$401>= E1)*(Sheet1!$B$1:$B$401<= E2))),
"input text or response you want here")
Change "input text or response you want here" to what you want to respond. If you just want it to return a numerical value, like 0, you can remove the quotes at the end and the text response and just put the number. Don't forget the comma (,) after the bracket though!
It should look like this:
=IFNA(SUMPRODUCT(FILTER((Sheet1!$A$1:$A$401,
(Sheet1!$B$1:$B$401>= E1)*(Sheet1!$B$1:$B$401<= E2))),0)
I do NOT recommend IFERROR because it will suppress all type of errors. And that is not a good practice.
What you should be doing is...
IF the filter returns 0 items, THEN just display 0
ELSE, display sum of all the items.
=IF(COUNT(FILTER(_range_,_condition_)) = 0, 0, SUM(FILTER(_range_,_condition_)))