How to combine column using excel4node with for loop - javascript

I'm trying using excel4node to covert my data to excel.
here is data :
[ 'AA', 'BB', 'CC' , 'DD'...]
I want each string is in 2 column in 1 row like this:
and now i'm stacking in the first row, i don't know how to combine the column.
here is what i trying to do is using forEach to iterate the data:
data.forEach((title, index) => {
sheet.cell(1, index + 1, 1, index + 2, true).string(title);
});
and it always shows like this:
seems it overwrite BB and i don't know how to avoid it.
According the terminal it shows :
"Invalid Range for: B1:C1. Some cells in this range are already included in another merged cell range: A1:B1."
and i also don't know how to write right code for 'count' column
please help me finish this excel column,

Related

Cannot get values of a column with getRange and getValues

Here is the code I have so far:
var items = data.getRange(1,14,1,lc).getValues()[0];
var names = cdata.getRange(52, 1, 28,1).getValues()[0];
When I log 'items' I get the desired information populated in a single array.
When I try to get the 'names' in a column using a similar method I can only get the first cell in a single array or all of the desired cells in an array of arrays.
example of items
[item1,item2,item3,...]
example of names
[name1] or [[name1],[name2],[name3],[...]]
Why am I not able to use the same method to accurately retrieve the names in a column as I did in the row? How do I fix this and get the desired result? How do I get names to return the data in the cells as I did with items?
I see you have 1 column for names combined with []. That will return a [] result from a a structure like [ [][][] ] (not [ [] ]). If your items have multiple rows, you'll get the entire first row [,,,], as you suggest in items.
var items = data.getRange(1,14,1,lc).getValues()[0];
var names = cdata.getRange(52, 1, 28,1).getValues()[0];
That's more a diagnosis than an answer. Basically, you're getting what you should get from that, but I think you want it transposed. Various ways to do that.
But will this work?
// flatten(flatten()) works for 2-level nesting, and so on
function flatten(arrayOfArrays){
return [].concat.apply([], arrayOfArrays);
}
reference: https://gist.github.com/MauricioMoraes/225afcc9dd72acf1511f
You will have to use this instead:
var names = cdata.getRange(52, 1, 28,1).getValues().flat();

datatables Column width trying to divide evenlly

Working on the Datatables Code and I had specified the like this:
{className: "editable alignCenter", "targets":"_all"}
Basically all my columns are coming dynamically, so i want to target the editable only the column which are , i had a dataattribute where i can identify the field is primarykey or identity field.
So i want to modify that if that field is defined, leaving that field, all other fields should be editable and that field can be left out
trying like this
var editableTargets = $('#layout').attr('data-array'); - this is coming as a comma seperated list like 0,1,2,3,4,5
"columnDefs": [
{className: "editable alignCenter", "targets": [editableTargets]}
]
but it seems to be not working
#andrewjames Thanks for the quick tip, it is working,
Now i am trying to find the last array from the editableTargets and remove the class editable aligncenter and add a different class to it and add a sorting: false to it
just give me a start
Regarding your updated notes:
Now i am trying to find the last array from the editableTargets and remove the class editable aligncenter and add a different class to it and add a sorting: false to it
Again, I may have misunderstood, but... here are some pointers to help you:
Assume you have this:
var editableTargets = "0,1,2,3,4,5";
The following code will split this into two arrays - one containing only the final element, and the other containing everything else:
// convert to an array of numbers:
var array = editableTargets.split(",").map(Number);
// remove the last element, and capture it as "i":
var i = array.pop();
// create an array containing the last element:
var lastItem = [];
lastItem.push(i);
So, array is [0,1,2,3,4] and lastItem is [5].
There may be better ways to do this in JavaScript, but this should work.
Now you should be able to use these in your column defs section - something like this (but I have not tested this part!):
"columnDefs": [
{ className: "editable alignCenter", "targets": array },
{ sorting: false, "targets": lastItem }
]

getNumColumns() doesn't update?

Basically what I'm trying to do is insert a certain number of values into a spreadsheet via a script. The way I have it set up, it inserts each value separately, calling the function x amount of times.
Let's say I have 4 values. I want to insert all the values into row 1 then, column 1, column 2, column 3, column 4. What's happening right now in my code, is it inserts all of the values into column 1 only, and it just overwrites anything already in column 1, so I only end up with the last value.
var column = sheet.getDataRange().getNumColumns() + 1;
sheet.getRange(1, column).setValue(question);
If I add in that + 1 to getNumColumns, it will add all four values, but into columns 2, 3, 4, and 5. So basically, if I don't use the + 1, the getNumColumns() never updates, and each time a value is inserted it stays at 1. I have no idea why this is and have been searching for quite some time now for an answer.
EDITED CODE:
var range = sheet.getDataRange();
var data = range.getValues();
if (range.isBlank()){
var column = 1;
sheet.getRange(1, column).setValue(question);
}else{
var column = sheet.getDataRange().getNumColumns();
sheet.getRange(1, column).setValue(question);
}
It looks like your data range initializes as column 1, meaning that the first time through you add 1+1 to get column 2, etc. The simplest approach is probably to write an if-statement so that when getDataRange().getNumColumns() == 1, you don't add the 1, but you add it every other time. Even better would be finding a way to initialize the data range to 0 instead of 1, if the API allows that.
Edited: On second thoughts, the safer way would be to check to see if the data range is blank, rather than if the number of columns is 1. You can use the isBlank() method for that.

SlickGrid: Get the total no of rows after Grouping

I am grouping the slickgrid table, and I want to know the no.of rows after grouping.
I found this solution
grid.getData().getPagingInfo().totalRows
from this question.
But it is not working for the table after grouping.
It returns the total no.of rows of the data not the updated no.of rows after grouping.
Please help me to solve this
Not sure if I understand your question right, but to me it seems that you are confusing Slick objects here. The grid.getData() will return the complete datasource of your grid.
If you take a look at the official SlickGrid example-grouping demo, you will see a grid with grouped set of datas. You can try yourself by opening your favorite browsers Javascript debugger:
dataView.getGroups()
It will return you the corresponding Array of objects from the dataview that contains all groups informations like:
Array [ Object, Object, ... , another 15... ]
You can reach these objects thru specifying the given element key of the returned array:
dataView.getGroups()[0]
Return value of command:
Object {__group: true, level: 0, count: 1, value: 0, title:
"Duration: 0 1, groups: null, groupingKey: "0"}
Now we can see that it has a count property which contains the exact number of rows belong to that group.
So finally by issuing:
dataView.getGroups()[0].count
Will return you:
1
Or by issuing:
dataView.getGroups()[1].count
Will return you:
3
Where both returned count numbers equal with the record numbers you can see in the grid.

storing table cell content into js array

I need to store table cell content into array using tr and td indexes like this:
myArray[tr_idx][td_idx]='value';
It is not required to store data of all of cells in array but only several with a special content, but if i use indexes as a keys of the array i'll have many empty array elements.
e.g.
myArray[2][3]='data1';
myArray[4][3]='data2';
alert (myArray.toSource()) -> [, , [, , , "data1"], , [, , , "data2"]]
maybe there is another suitable way of storing such type of data?
Why not just store them as individual 2-item arrays [row,col]? Since the table cells are already accessible via tableElement.rows[].cells[], you can use the 2 indices to access them from the table.
var storedCells = [];
// Store row 2, column 3
storedCells.push([2,3]);
Access with:
// Table row:
storedCells[0][0]
// Table column
storedCells[0][1]
//As in :
tableElement.rows[storedCells[0][0]].cells[storedCells[0][1]].innerHTML = "New cell value!"
Or even cleaner, if you prefer to use objects rather than arrays:
storedCells.push({row: 2, column: 3});
storedCells.push({row: 4, column: 6});
Accessed with:
tableElement.rows[storedCells[0].row].cells[storedCells[0].column].innerHTML = "New cell value!";
Finally, if you don't actually need the row/column indexes, but rather the DOM nodes themselves, just push those onto your array.
var storedCells = [];
// Save references to some individual <td> nodes
storedCells.push(tableElement.rows[1].cells[2]);
storedCells.push(tableElement.rows[4].cells[6]);
They are then trivially modified:
// Set the value of the second element:
storedCells[1].innerHTML = "New content!";
Update after comment:
If you need to be able to attach the cell coordinates to a new value from the backend, a good solution would be to expand the object {} example above to include a value attribute. The server can pass back the new value in JSON.
storedCells.push({row: 2, column: 3, value: ""});
storedCells.push({row: 4, column: 6, value: ""});
Your backend should send a JSON response back to the script containing the same type of array of objects, where value: has been populated with the new cell value. You can then pass it back into the table with:
// Pass the value attribute received from JSON into the cell's contents
tableElement.rows[storedCells[0].row].cells[storedCells[0].column].innerHTML = storedCells[0].value;
Not sure if this is the right answer but you could try to check if cells are empty before pushing them into the array. In the loop place a an if statement that will check the value of the cell and compare it to the value you require. This way you should end up with just an array of value and no empties. But you may need to record the index as well then.

Categories

Resources