Exceljs How to addRow and merge cells - exceljs

So I need to generate an excel spreadsheet based on data from DB. Once I add in all the data from the DB I need to add in a new row at the bottom that says when the spreadsheet was generated like so:
I don't know how to get the cells to merge here, I can't use worksheet.mergeCells() since I don't know how many rows of data there will be in the DB. How would I merge the cells when using worksheet.addRow()?

I have come across a similar problem and have used something close to this:
const newRow = worksheet.addRow(["This report is..."]);
const currentRowIdx = worksheet.rowCount; // Find out how many rows are there currently
const endColumnIdx = worksheet.columnCount; // Find out how many columns are in the worksheet
// merge by start row, start column, end row, end column
worksheet.mergeCells(currentRowIdx, 1, currentRowIdx, endColumnIdx );

Related

Create an array where the elements are determined by non null values in a row

I have the following sheet to track participation in events...
... that continues for hundreds of columns and rows. A blank cell indicates that person did not participate in that event. I can not change the layout of this sheet.
For each name I need to create an array of Event IDs for the events they have participated in, which would look like this:
Columns would be fine too.
I am already able to generate this array using (pseudo-formula) QUERY(TRANSPOSE(range), "SELECT (Event ID column) WHERE (user column) IS NOT NULL", 0), however I'm looking for a solution that does not need to TRANSPOSE the sheet, either in memory or in a separate 'helper' sheet.
Give a try on below formula.
=FILTER($C$1:$G$1,FILTER($C$3:$G$6,$A$3:$A$6=A8)<>"")

How to get row List elements compare by two rows Google Sheets

I have spreadsheet https://docs.google.com/spreadsheets/d/1qjvn90lZ7AWhYApChd2gAKHzZqmnNz4xlURENSQasaw/edit#gid=0 and i want to get rows with some differences by unique values Id and Updated at.
List №1 i have the same automatic importing data http://prntscr.com/t3axvt
In List №3 i try to use =UNIQUE('List1'!A2:A;'List1'!D2:D) http://prntscr.com/t3ayx8 but it didn't work
Question
i need to get rows from List1 if there are duplicates with these parameters Id and Updated at first row from duplicate rows (must be like this http://prntscr.com/t3b3nb) or last row from duplicate rows (must be like this http://prntscr.com/t3b3nb).
You can create a helper column to achieve this
Create a helper column J and put the below formula in J2
=arrayformula(if(D2:D7=OFFSET(D2:D7,-1,0),"",ROW(A2:A7)))
Then you'll be able to filter your data, put below formula in A10
=FILTER(A2:J7,J2:J7<>"")
Please amend your data ranges per your requirement
Please use ; instead of , if you in are different continent
for Extended Range, use below formula
=arrayformula(if(D2:D="","", if(D2:D=OFFSET(D2:D1000,-1,0),"",ROW(A2:A))))

Script to copy formulas when inserting row at the top - Google Sheets

I have a call log that has multiple drop-downs and formulas for making the data entry easier for the people using it. The problem is that when I insert a row (in row 2 under header) the data validation remains but the formulas don't copy. I have tried several scripts but none of them seem to work. I have about 10 columns with formulas in them.
After reading your question and studying your example, I assume the following:
You want to add a new row after the headers.
You desire to keep the formulas and data validation rules in the old rows.
You wish to copy the formulas of the old rows into the new one.
If my assumptions are correct, you can use the following example to fulfill your requests:
CODE
function addRowAtTop() {
var sheet = SpreadsheetApp.openById(
'{SPREADSHEET ID}').getSheetByName(
'PNC Calls');
sheet.insertRowAfter(1);
for (var i = 1; i <= 34; i++) {
var cellFormula = sheet.getRange(3, i).getFormula();
if (cellFormula != '') {
sheet.getRange(2, i).setFormula(cellFormula);
}
}
}
BEHAVIOUR
The previous code will add a row at the top, and after that it will check each cell of the old first row. If it encounters a formula, it will copy it to the new first row.
ALLUSIONS
getFormula()
Please take this as one of the possible solutions to your issue, and don't hesitate to write me back with any additional doubts or requests to further clarifications.

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.

Creating a Macro in google spreadsheet to search and then write text

What I am trying to accomplish is I would like to search for a term in one cell, if that cell has the term write text to another cell. My specific example would be I would like to search for the term 'DSF' in column 4. If I find 'DSF' it would then write 'w' in column 5 & write '1.2' in column 3. This is searched per row.
I do understand the the .setvalue will write the needed text, but I do not understand how to create a search function. Some help would be greatly appreciated.
EDIT
Here is the code I am working with at the moment. I am modifying it from something I found.
function Recalls()
{
var sh = SpreadsheetApp.getActiveSheet();
var data = sh.getDataRange().getValues(); // read all data in the sheet
for(n=0;n<data.length;++n){ // iterate row by row and examine data in column D
if(data[n][3].toString().match('dsf')=='dsf'){ data[n][4] = 'w'}{ data[n][2] = '1.2'};// if column D contains 'dsf' then set value in index [4](E)[2](C)
}
//Logger.log(data)
//sh.getRange(1,1,data.length,data[3].length).setValues(data); // write back to the sheet
}
With the Logger.log(data) not using the // It works properly but it overwrites the sheet, which will not work since I have formulas placed in a lot of the cells. Also, Maybe I did not realize this but Is there a way to do a live update, as in once I enter text into a cell it will research the sheet? Otherwise having to 'run' the macro with not save me much time in the long run.
Try this. It runs when the sheet is edited. It only captures columns C,D,&E into the array and only writes back those columns. That should solve overwriting your formulas. It looks for 'DSF' or 'dsf' in column D (or contains dsf with other text in the same cell either case). Give it a try and let me know if I didn't understand your issue.
function onEdit(){
var sh = SpreadsheetApp.getActiveSheet();
var lr = sh.getLastRow()// get the last row number with data
var data = sh.getRange(2,3,lr,3).getValues(); // get only columns C.D,& E. Starting at row 2 thur the last row
//var data = sh.getDataRange().getValues();// read all data in the sheet
for(n=0;n<data.length-1;++n){ // iterate row by row and examine data in column D
// if(data[n][0].toString().match('dsf')=='dsf'){
if(data[n][1].match(/dfs/i)){ //changed to find either upper or lower case dfs or with other text in string.
data[n][2] = 'w';
data[n][0] = '1.2'};
}
sh.getRange(2,3,data.length,data[3].length).setValues(data); // write back to the sheet only Col C,D,& E
}

Resources