consolidate specific row values from different sheets - google-apps-script

I'm looking for a way to consolidate specific row values from multiple spreadsheets when certain validation are met.
Scenario: I currently have Sheet1, Sheet2, Sheet3
what I want is to consolidate the data from sheet2 and sheet3 into sheet1 when the validation is met.
Sheet1:
Sheet2:
Sheet3:
Based on the example above, I used this function in sheet1 cell A2
=iferror(query(Sheet2!A2:E, "where E = 'New Player' ",0),"")
so what happens is it automatically adds the row values from Sheet2 if Column E "Type" is "New Player" (validation).
What I want is I would also like to add sheet3 row data if its type is "New Player".
maybe there's a way to nest query function? or can this be done using script? I found some scripts that is able to copy rows but without the validation and not from multiple sheets.

Assuming you are querying within the same spreadsheet, something like this should work
=iferror(query({Sheet2!A2:E; Sheet3!A2:E}, "where Col5 = 'New Player' ",0),"")
EDIT: And if you want to pick which columns to return (example A, B, C but not the rest)
=iferror(query({Sheet2!A2:E; Sheet3!A2:E}, "Select Col1, Col2, Col3 where Col5 = 'New Player' ",0),"")

Related

Google Appscript partial vlookup

I am very new to appscript and any help on the below query will really be helpful.
I am having data in Sheet1 in column A and Column B, now I want to design an appscript which get the below job done.
(1) Sheet1 Column A has Roll number along with student name i.e. Roll No.*Name, column B has actual value (these both columns are user input).
(2) Sheet2 has two columns Roll No. and Dummy value.
(3) In Sheet1 column C, I want the dummy value from Sheet2 column B by matching the Roll number of both sheets, however, I want value only in those rows in which actual value (column B) in Sheet1 is blank.
For reference I am sharing the link of the sheets.
https://docs.google.com/spreadsheets/d/1vI22QCmixKe3aoWMLODTFzt7pNXIKO3pjXS4mT6GHT0/edit#gid=524973836
I had checked different question on this community but not found a solution to my query.
Any help on above will really be appreciated.
Regards
Assume you want to put down 'dummy values' in Sheet1 column C if column B is empty, and that so call 'dummy value' need to has its 'Roll No.' value matched with the number of the front part of 'Roll No.#Name' column in sheet1:
Solution - 1. in-app lookup function:
Put this code into Column C2 in sheet1.
This code lookup for matches of 'Roll No.' from sheet2 column A whenever there Column A is not empty and Column B is empty in sheet1.
=ArrayFormula(
IFS(
$A$2:$A="","",
$B$2:$B<>"","",
TRUE,XLOOKUP(INDEX(SPLIT($A$2:$A,"*"),,1),sheet2!$A:$A,sheet2!$B:$B)
)
)
Solution - 2. write a custom function in apps-script:
in-app call:
apps-script js code:
this solution create a custom function called 'MYLOOKUP()' with apps-script, and call the function form the spreadsheet.
// apps-script:
function MYLOOKUP(data1,data2) {
return data1
.map(([rollNo_Name,value]) => {
return (rollNo_Name !== '' && value === '') ?
data2.find(([rollNo,]) => rollNo_Name.split('*')[0] == rollNo)[1] :
''
});
}
in-app call:
=MYLOOKUP(A2:B,sheet2!A:B)
There are a lot other solution to your question if you mention apps-script, such as...
instead of calling the custom function in-app, you can instead create a custom menu to run the function when you click on it,
or add simple trigger so that every time the spreadsheet is edited, the function will be run one time.
you can even create a web ui for this thing to run, etc.

Preserve associated row data while using importRange in the destination sheet

For our construction company, we have a sheet that has all of the bills listed, along with relevant data that our accounting person would add to the master sheet.
I then have another sheet that pulls this data for the relevant people in the accounts for them to complete those steps. It filters to only the relevant columns (specifically, based on Column H - either "Yes" or "No") using query and importRange.
query(IMPORTRANGE("https://docs.google.com/spreadsheets/d/1pY53-XaGnUQ3BPmLh90mLSqIwSo7S2_QOPbD6JBQHOA/edit#gid=0","Master!A3:G"), "Select Col1, Col2, Col4, Col5, Col6, Col7 where Col6 is not null")
I want to include a few details in the destination sheet, which I have done.
The problem is typically associated with column H in the master sheet (Work Done required or not). For most cases, it is either a yes or no. However, in some cases, the accounting person doesn't know for sure whether it is a yes or no. But he wants to keep on adding other bill details.
When he fills the empty column later, the entered data on this second sheet doesn't dynamically shift with the imported data, thus causing the rows to misalign.
Unfortunately, as I mentioned, the rows don't stick together so as the dynamic order of the imported columns changes, the static order of the manual columns causes a mismatch.
Is there a way to make this work?
A solution would be to add an onEdit trigger in order to check if any changes have been made to the H column and later add the corresponding rows to the destination sheet, something similar to this:
function onEdit(e) {
let destinationSheet = SpreadsheetApp.openById("SS_ID").getSheetByName('Input Sheet');
let sourceSheet = SpreadsheetApp.getActiveSheet();
let lastRow = destinationSheet.getLastRow();
if (e.range.getColumn() == 8 && e.range.getValue() != null) {
let billNo = sourceSheet.getRange(e.range.getRow(),1).getValue();
// retrieve all the other values needed from the current row of the edit
// current row of the edit > e.range.getRow()
destinationSheet.getRange(lastRow+1,1).setValue(billNo);
// copying the values to the destination sheet by using setValue()
}
}
The above code makes use of the e event object in order to check if the edit has been made in the H column and if the value is different from null. If this condition checks, the values needed to be copied are being retrieved (here I have illustrated how to retrieve the billNo) and later set them in the destination sheet by retrieving the last row of it.
Note
You can also add a for loop in order to manage the copying the data easier and also add more conditions such that it the new entry is being pasted after a certain Actual Date or simply sort the values after the entry is being pasted.
Reference
Apps Script Triggers;
Apps Script Event Objects;
Apps Script Range Class.
I have performed testing with the following formula, I found no issue even I remove certain value for certain cell in "Col H", and the result is still correct, am I miss out certain information? I just change the filter criteria based on Col H only
=query(IMPORTRANGE("https://docs.google.com/spreadsheets/d/1pY53-XaGnUQ3BPmLh90mLSqIwSo7S2_QOPbD6JBQHOA/edit#gid=0","Master!A3:H"),"Select Col1, Col2, Col4, Col5, Col6, Col7 where Col8 is not null")

Get country name from a range of IP number with google sheets script and paste the result in another column

In my Google spreadsheet, in column H I have a list of IP numbers. What I need to a script what will take those numbers and fetch the Country name. I need this to be script since the spreadsheet gets populated from a web-form. What I have is a formula that does that perfectly but I have to copy the formula manually with each form submission. Since I get lots of submissions per day, it's tedious to do this manually. I need the script to run on spreadsheet update. The formula I have is this:
=query( importhtml("http://whatismyipaddress.com/ip/" & H1526, "table", 2), "select Col2 where Col1 = 'Country:' ", 0 )
The last populated row is H1526. I tried the "ARRAYFORMULA" but no array formulas seems to work with my query function. The column where I have the IPs is H and I need the country name in column I.
You can write script on submit of the Google form. Also you need to set a trigger for the same. Hope this helps.
function OnSubmit(e){
var sheetDatabase = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
sheetDatabase.getRange(sheetDatabase.getLastRow(), 8).setFormula('=query(importhtml("http://whatismyipaddress.com/ip/"&I'+sheetDatabase.getLastRow()+', "table", 2), "select Col2 where Col1 = \'Country:\' ", 0 )');
}

Google Sheets Script lookup

I'm trying to make a script in google sheets that make this:
I have a database with "id" and "product" in the sheet named "base":
In another sheet I have the "id2" and the "product2":
I need to create a Script that search (vlookup I think) the "id2" in the first database and bring 'yes' or 'no' according to the product2 and put it on the obs column
This logic can be expressed by double application of filter: first, filter the columns of Sheet1 so that the column you get is the one with product name in it (B2 in your example); then filter the rows so you get the Id as in the cell A2.
=filter(filter(Sheet1!A2:D, Sheet1!A1:D1 = B2), Sheet1!A2:A = A2)

Function for getting a specific sheetname

I have a workbook with the following apperance:
This workbooks first sheet, "Resultat", calculates data from the other sheets in the workbook and presents them as the picture shows.
In order for the calculations to work correctly, the names shown in the marked red squares must be the same and corresponding, else the value will be ##### as you can see for entry number 2 (cell A5 works and have the same name as sheet number 2, but cell A8 do not work because it does not have the same name as sheet number 3).
My question is basicly, is it possible to use a function that will enter the name of a sheet in a specific cell. In this example, I would like the cell A5 automaticly fetch the name of the sheet with index 2, cell A8 be equal to sheet with index 3 and so on.
Right now im doing this manually but it would be a great help if this could be automated since I have alot of these workbooks and the names changes from time to time.
Use this formula
=MID(CELL("filename",A1), FIND("]", CELL("filename", A1))+ 1, 255)
The above will give you the name of the current sheet
If you change the reference of A1 to the relevant sheet then it will pick up that name.
Ex:
=MID(CELL("filename",Sheet1!A1), FIND("]", CELL("filename", Sheet1!A1))+ 1, 255)
This will give you Sheet1
Create a VBA module and enter the following code:
Public Function SheetNameByIndex(Index As Integer) As String
SheetNameByIndex = ActiveWorkbook.Sheets(Index).Name
End Function
now, at any place in your workbook, you can do
=sheetnamebyindex(2)
=sheetnamebyindex(A1)