I am trying to get count of last used row for a specific column, but was only able to get count of max rows occupied. Test data is shown in attached snap. Please help. Snap Attached Here
SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY");
ExcelFile ef = ExcelFile.Load("test.xlsx");
CellRange rcount = ef.Worksheets[0].Columns[1].Cells;
Console.WriteLine(ef.Worksheets[0].Rows.Count);
ef.Save("test.xlsx");
Console.ReadKey();
Cells are internally allocated in rows, not in columns. In other words, you could get the last used column for each row with ExcelRow.AllocatedCells.Count.
For getting the last used row in a specific column you can use something like the following:
ExcelFile ef = ExcelFile.Load("test.xlsx");
ExcelWorksheet ws = ef.Worksheets[0];
ExcelColumn column = ws.Columns[1];
int rowIndex = ws.Rows.Count - 1;
while (rowIndex >= 0 && column.Cells[rowIndex].ValueType == CellValueType.Null)
--rowIndex;
Console.WriteLine(rowIndex);
I hope this helps.
Related
I've got a PIVOT table output in SQL Server Management Studio. Every row contains data that are either filled or NULL. I need to fill the NULL values according to the following logic:
If value is NULL, then go further to the left (in the row) and fill it with the closest value to the left.
If there are no values to the left, then use the closest value to the right to fill it.
Thanks for your kind help,
Dave
Since you have a limited number of columns, coalesce() should do the trick
Select Product
,Time1 = coalesce(Time1,Time2,Time3,Time4,Time5,Time10,Time15,Time20,Time30)
,Time2 = coalesce(Time2,Time3,Time4,Time5,Time10,Time15,Time20,Time30,Time1)
,Time3 = coalesce(Time3,Time4,Time5,Time10,Time15,Time20,Time30,Time1,Time2)
,Time4 = coalesce(Time4,Time5,Time10,Time15,Time20,Time30,Time1,Time2,Time3)
,Time5 = coalesce(Time5,Time10,Time15,Time20,Time30,Time1,Time2,Time3,Time4)
,Time10 = coalesce(Time10,Time15,Time20,Time30,Time1,Time2,Time3,Time4,Time5)
,Time15 = coalesce(Time15,Time20,Time30,Time1,Time2,Time3,Time4,Time5,Time10)
,Time20 = coalesce(Time20,Time30,Time1,Time2,Time3,Time4,Time5,Time10,Time15)
,Time30 = coalesce(Time30,Time1,Time2,Time3,Time4,Time5,Time10,Time15,Time20)
From ...
Pivot ...
Hello i am trying to write an existing xlsx file using phpspreadsheet with setActiveSheetIndexByName(sheetname) and setcellvalue with reference and value, but it updates only the last record. spent more than 12 hours on this.
i tried foreach instead of while and used a counter to increment, but none worked.
<?php
include_once('db.php');
$prospect = $_REQUEST['prospect'];
require 'vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
$sql1 = mysqli_query($db,"select filename,sheetname, row, responsecol,compliancecol,response, compliance from spreadsheet where `prospect`='$prospect' and response <>'' order by row");
//$row=1;
while($row1 = mysqli_fetch_assoc($sql1))
{
$filename= $row1['filename']; //test.xlsx
$sheetname= $row1['sheetname']; // mysheet
$responsecol= $row1['responsecol'].$row1['row']; //D1
$response= $row1['response']; //response
$compliancecol= $row1['compliancecol'].$row1['row']; //C1
$compliance= $row1['compliance']; //compliance
$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($filename);
$spreadsheet->setActiveSheetIndexByName($sheetname)
->setCellValue($compliancecol,$compliance)
->setCellValue($responsecol,$response);
//$row++;
}
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save("newfile.xlsx");
exit;
?>
i wish each of the row from mysqli result updates each reference cell with value.
The easys way is to set a Limit of 1 to your MySQL query. That takes only one value from your data. If you will the last you should sort DESC.
$sql1 = mysqli_query($db,"select filename,sheetname, row, responsecol,compliancecol,response, compliance from spreadsheet where `prospect`='$prospect' and response <>'' order by row DESC LIMIT 1");
Using SSIS I am bringing in raw text files that contain this in the output:
I use this data later to report on. The Key columns get pivoted. However, I don't want to show all those columns individually, I only want to show the total.
To accomplish this my idea was calculate the Sum on insert using a trigger, and then insert the sum as a new row into the data.
The output would look something like:
Is what I'm trying to do possible? Is there a better way to do this dynamically on pivot? To be clear I'm not just pivoting these rows for a report, there are other ones that don't need the sum calculated.
Using derived column and Script Component
You can achieve this by following these steps:
Add a derived column (name: intValue) with the following expression:
(DT_I4)(RIGHT([Value],2) == "GB" ? SUBSTRING([Value],1,FINDSTRING( [Value], " ", 1)) : "0")
So if the value ends with GB then the number is taken else the result is 0.
After that add a script component, in the Input and Output Properties, click on the Output and set the SynchronousInput property to None
Add 2 Output Columns outKey , outValue
In the Script Editor write the following script (VB.NET)
Private SumValues As Integer = 0
Public Overrides Sub PostExecute()
MyBase.PostExecute()
Output0Buffer.AddRow()
Output0Buffer.outKey = ""
Output0Buffer.outValue = SumValues.ToString & " GB"
End Sub
Public Overrides Sub Input0_ProcessInputRow(ByVal Row As Input0Buffer)
Output0Buffer.AddRow()
Output0Buffer.outKey = Row.Key
Output0Buffer.outValue = Row.Value
SumValues += Row.intValue
End Sub
I am going to show you a way but I don't recommend adding total to the end of the detail data. If you are going to report on it show it as a total.
After source add a data transformation:
C#
Add two columns to your data flow: Size int and type string
Select Value as readonly
Here is the code:
string[] splits = Row.value.ToString().Split(' '); //Make sure single quote for char
int goodValue;
if(Int32.TryParse(splits[0], out goodValue))
{
Row.Size = goodValue;
Row.Type = "GB";
}
else
{
Row.Size = 0;
Row.Type="None";
}
Now you have the data with the proper data types to do arithmatic in your table.
If you really want the data in your format. Add a multicast and an aggregate and SUM(Size) and then merge back into your original flow.
I was able to solve my problem in another way using a trigger.
I used this code:
INSERT INTO [Table] (
[Filename]
, [Type]
, [DeviceSN]
, [Property]
, [Value]
)
SELECT ms.[Filename],
ms.[Type],
ms.[DeviceSN],
'Memory Device.Total' AS [Key],
CAST(SUM(CAST(left(ms.[Value], 2) as INT)) AS VARCHAR) + ' GB' as 'Value'
FROM [Table] ms
JOIN inserted i ON i.Row# = ms.Row#
WHERE ms.[Value] like '%GB'
GROUP BY ms.[filename],
ms.[type],
ms.[devicesn]
I have a cell array that I call "Table", as in the code below (but my array has more lines). Column 1 contains dates in string format. I want to add an additional column that contains the dates in datetime format. I did the following, which works, but it is VERY slow. What are the alternatives?
% Table that I have:
Table{1,1} = 'Stringdate';
Table{2,1} = '01.01.1999';
Table{3,1} = '02.01.1999';
Table{4,1} = '03.01.1999';
Table{5,1} = '04.01.1999';
% What I want to add:
Table{1, size(Table,2)+1} = 'Datetime';
for index = 2:length(Table)
Table{index, size(Table,2)} = datetime(Table{index, 1});
end
You can apply datetime to all of them in one-go and use just num2cell and indexing to achieve the same result as that of your loop.
Table(2:end,2) = num2cell(datetime(Table(2:end,1)));
%You might need to specify the InputFormat as well i.e.
%Table(2:end,2) = num2cell(datetime(Table(2:end,1),'InputFormat','dd.MM.yyyy'));
I have a flexgid with 3 columns. I need to make the first column a checkbox column so that users can check and uncheck there. And i need the other column values which are checked. ` MSFlexGridBrg.Width = "3200"
For Bea = 1 To NbBearg
MSFlexGridBrg.Rows = NbBearg + 1
MSFlexGridBrg.Cols = 3
'MSFlexGridBrg.CellFontName = "Wingdings"
MSFlexGridBrg.TextMatrix(0, 0) = "Select"
If MainUnitIn Then
MSFlexGridBrg.TextMatrix(0, 2) = "Cu (N)"
Else
MSFlexGridBrg.TextMatrix(0, 2) = "Cu (lbf)"
End If
MSFlexGridBrg.Row = 1
MSFlexGridBrg.col = 1
MSFlexGridBrg.TextMatrix(0, 1) = "Bearing No."
MSFlexGridBrg.Row = Bea
MSFlexGridBrg.Text = Bea & ". "
MSFlexGridBrg.col = 1
MSFlexGridBrg.TextMatrix(MSFlexGridBrg.Row, 2) = Cu_Value_Estimate(Bea)
Next`
The output is attached as the image. I want the column named Select as the Checkbox column.
You can't do that in your current first column, as it is a fixed column which doesn't allow edit. Add a new column and inside your formatting loop (which isn't clearly to see in your code sample, but i see there is the next at the very bottom) use a standard formatting code:
If myBooleanVar = True Then
MSFlexGridBrg.TextMatrix(i, 0) = "Yes"
Else
MSFlexGridBrg.TextMatrix(i, 0) = "No"
End If
If you need to use the first cell of the column to select all the rows, you may use the MSFlexGridBrg_Click() and then check for MSFlexGridBrg.MouseCol = -1 and MSFlexGridBrg.MouseRow = -1 - but a checkbox won't never appear.