SpreadsheetApp insert a new column with header and formula - google-sheets

I need to insert a column after 5th column with a header(Average Clicks) also the values in this column must be calculated based on a formula (=E4*F4).
I can insert column after 5th by using this
sheet.insertColumns(5, 1);
Is it possible to create with header and also need to set the formula.
Thanks

You can use the ArrayFormula function to fill the formula into multiple rows.
function insertColumns() {
var sheet = SpreadsheetApp.getActiveSheet();
sheet.insertColumnAfter(6).getRange(1, 7).setValue("Average Clicks");
sheet.getRange(2,7).setFormula("=ArrayFormula(E2:E*F2:F)");
}

Related

Match lookup value with importHTML and combine tables in Soogle Sheet

I have total 2 url links both of which match lookup value and want column of second table in first table.
I have first taken a table from the importhtml formula. And the cell that is green is the column lookup value. Which is as follows.
1st table https://www.screener.in/screens/881782/rk-all-stocks/?page=21
=QUERY(IMPORTHTML("https://www.screener.in/screens/881782/rk-all-stocks?page=1", "table",1),"where Col1 is not null",1)
And there is another table in which I want to match the lookup values from the first table and all the values from that table. Which I have tried. Within this google sheet and you can also try in this google sheet.\
=VLOOKUP(B3,IMPORTHTML("https://www.screener.in/screens/881791/rk-holding/?page=1", "table",1),12,0)
2nd table https://www.screener.in/screens/881791/rk-holding/?page=1
In column next to roce I want column of promoter holding. Which you have to take from this url link.
get result like
try this in google sheet. : https://docs.google.com/spreadsheets/d/1yawdkBHkheaXeziWHFBFd3gngO3EQJr0d19lUH9cVfI/edit?usp=sharing
Thanks in Advance.
added formula to your sheet
Cell L3:
=BYROW(B3:B,LAMBDA(bx,IF(bx="",,IFNA(QUERY(IMPORTHTML("https://www.screener.in/screens/881791/rk-holding/?page=1", "table",1),"Select Col12, Col13 Where Col2='"&bx&"'",0)))))
-

How to get list of row that has value from a range of data based on column header as the criteria?

I have a range of data populated from a google form in sheet "Data".
I need to make a report (in sheet "Report") that shows the students attendance, based on name (from drop down list). The data that is blank is excluded in that report, as seen in column i to column k
desired result
I have shared the sheet here
I have tried to use filter
=filter(Data!B1:I20,Data!E1:E20<>"")
but still I cant find the way to change the "condition" in this formula to refer to the sheet Report A1.
what is the correct formula to achieve this?
Thanks you for your help.
Using Apps Script you will have to use the function getSheetByName(name).
Example:
// The code below logs the index of a sheet named "Expenses"
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Expenses");
if (sheet != null) {
Logger.log(sheet.getIndex());
}
Then to find from column E the rows that have value you would have to use the getRange(a1Notation) method:
// Get a range A1:D4 on sheet titled "Invoices"
var ss = SpreadsheetApp.getActiveSpreadsheet();
var range = ss.getRange("Invoices!A1:D4");
// Get cell A1 on the first sheet
var sheet = ss.getSheets()[0];
var cell = sheet.getRange("A1");
Then you would have to iterate over that rows and get the rows where the E column has something.
Once you have that you can get the Report sheet and set the values there with setValues(values)
For more information on how the function you may need to use you have the reference of SpreadsheetApp and the overview on Extending Google Sheets

Transpose unique data from column into 1 row in new sheet with more than 1 separator columns with headers

While searching for a function to use, I found the below formula to adapt from.
=TRANSPOSE(ArrayFormula(TRIM(SPLIT(QUERY(A1:A30&"|"&rept("|",1),,2^99),"|",1,0))))
What has worked so far is the use of the below formula but
I can't seem to change it to more than 1 separator column.
={(ArrayFormula(TRIM(SPLIT(QUERY(UNIQUE(A2:A)&"|"&rept("|",1),,3^99),"|",1,0))))}
Also, is it possible to add headers to the currently empty separator columns in the sequence of example: "qty","expiry","available qty" before the next column and repeat again "qty","expiry","available qty"
Test spreadsheet in at cell I1 (Join function doesn't work as there's a limit):
https://docs.google.com/spreadsheets/d/1w64R9QR_pAvdGy7IZLsHFvhIYIGL7lqsl_vFXrqsQMc/edit#gid=799170483
Please use the following formula and adjust ranges to your needs:
={(ArrayFormula(TRIM(SPLIT(QUERY(UNIQUE(A2:A)&"|qty|expiry|available qty"&rept("|",1),,2^99),"|",1,0))))}
Functions used:
ArrayFormula
TRIM
SPLIT
QUERY
UNIQUE

Remove rows based on duplicates in a single column in Google Sheets

I have spreadsheet similar to this:
I'd like to remove all duplicates of row based on the first column data.
So from this screenshot row, 1 and 2 would be kept, and row 2 would be removed. Any help would be greatly appreciated.
P.S. In my case I have columns from A to AU and rows from 2 to 9500.
Thank you.
Maya's answer and the solution AJPerez linked both work.
You can also use Filter View, which doesn't require deleting rows or creating new rows/sheets.
First create a helper column, say to the left of all your data. If your data starts on row 1, then create a blank row above all your data; if not, you are fine. Afterwards, on the first row where you have data, say row 2, write in the formula
=iserror(match(B2,B$1:B1,0))
Replace "2" with the row number of the first row of your data and "1" with that number minus 1. Also populate the rest of the column with the formula. (Unfortunately, arrayformula doesn't work here.) The formula outputs TRUE when the entry in B# has not occurred in cells above.
Note that this assumes your data now starts with column B and column B is where you want the filter to base. If that's not the case, just edit the column index too accordingly.
Select this new helper column. Go to Data -> Filter Views... -> Create a new Filter. Select filter by value and check TRUE only.
Caveat: this filter can only work if there are actually rows with TRUE value. This will always be the case as some entries are always unique.
If you want to avoid the caveat in your future applications, you can use filter by conditions with custom formula. The formula b2 should work.
But to begin with, even without the helper column, the above formula should work. Why doesn't it? That would be a good question for Google Support should it exist.
You can also use a google apps script to do this. To open the script editor from google sheets:
Choose the menu Tools > Script Editor.
Copy and paste the following script:
function removeDuplicates() {
var sheet = SpreadsheetApp.getActiveSheet();
var data = sheet.getDataRange().getValues();
var newData = [];
var ids = [];
for (var i in data) {
var row = data[i];
var duplicate = false;
if (ids.indexOf(row[0]) > -1) {
duplicate = true;
} else {
duplicate = false;
ids.push(row[0]);
}
if (!duplicate) {
newData.push(row);
}
}
sheet.clearContents();
sheet.getRange(1, 1, newData.length, newData[0].length).setValues(newData);
}
This assumes that you want to dedupe based on the contents of the first row of you sheet. If not you can adjust the row references from the 0 index to any other index you wish.
I would...
Create a new sheet
For column A, do =Unique(Sheet1!A:A)
Simply use VLOOKUP to populate the other columns. This will deliver the first value associated with the duplicates.

Merging data in google sheets from multiple sheets with varying number of rows

I have multiple google sheets with each containing variable number of rows but fixed set of columns. The number of rows can change in any sheet with time. How can i create summary sheet with data from all sheets placed one after the other? eg. If sheet 1 has 5 rows and sheet 2 has 6, i want the summary sheet to have 11 rows with 1-5 containing data from sheet 1 and 6-11 data from sheet 2.
= {
FILTER(NamedRange1, NamedRange1Col1 <> '');
Filter(NamedRange2, NamedRange1Col1 <> '')
}
Please create a named range for each sheet. This ranges will comntain your data.
Within each named range, also create a named range column. This can be the first column of every named range, or any column.
We are using the filter function FILTER so that you do not import empty rows. You dont have to use it though. The curly braces are semicolon are the crucial parts. In general, you "stack" multiple arrays as follows:
= {Array1; Array2;...;Array n}
the correct syntax is:
={FILTER(Sheet1!A:C; Sheet1!B:B<>"");
FILTER(Sheet2!A:C; Sheet2!B:B<>"")}
and another solution would be:
=QUERY({Sheet1!A:C; Sheet2!A:C}; "where Col2 is not null", 0)

Resources