Dynamically set column in sum formula using aspose.cells - aspose

I am use aspose excel export. I am setting formulla in .net.
I want sum of two cells value in third cell. like
excelbook.Worksheets[0].Cells["A2"].R1C1Formula = "=SUM(R1C1,R1C2)";
but i want to set the column dynamically. like in above formulla column C1,C2 are fixed.
now i want pick the column name dynamically , i want to build my formulla like below
excelbook.Worksheets[0].Cells["A2"].R1C1Formula = "=SUM(R1C(Value of Cell B1),R1C(Value of Cell B2))";
so if i enter 1 in B1 cell and 3 in B2 cell then it should sum A1 and C1.i can enter any value in B1 and B2 cell and excel should calculate the sum based upon entered values.
Please suggest how to build this type of formulla.
directly in excel we can do by this way:-
=INDIRECT(B1&1)+INDIRECT(B2&1)
so please suggest can i use INDIRECT in r1c1 formulla or suggest some other way.

If you want to add D1 and E1 in Excel but you want to select the row based on the value of the particular cell; for example, in this case, you might use A1 and A2 to place the value of the row to select, then you will use the formula like this:
=SUM(INDIRECT("D"&$A$1),INDIRECT("E"&$A$2))
You can assign the same formula using Aspose.Cells for .NET as well. Please see the following code:
//create XLS File
//Instantiate a Workbook object that represents Excel file.
//create a workbook
Workbook CellsDocument = new Workbook();
//Note when you create a new workbook, a default worksheet
//"Sheet1" is added (by default) to the workbook.
//Access the first worksheet "Sheet1" in the book.
Worksheet sheet = CellsDocument.Worksheets[0];
//Access the cells and put values
Aspose.Cells.Cell cellA1 = sheet.Cells["A1"];
cellA1.PutValue(1);
Aspose.Cells.Cell cellA2 = sheet.Cells["A2"];
cellA2.PutValue(1);
Aspose.Cells.Cell cellD1 = sheet.Cells["D1"];
cellD1.PutValue(4);
Aspose.Cells.Cell cellE1 = sheet.Cells["E1"];
cellE1.PutValue(5);
Aspose.Cells.Cell cellE14 = sheet.Cells["E14"];
cellE14.R1C1Formula = "=SUM(INDIRECT(\"D\"&$A$1),INDIRECT(\"E\"&$A$2))";
//Save the Excel file.
CellsDocument.Save("output.xls", Aspose.Cells.SaveFormat.Excel97To2003);
Please try it at your end using the latest version of the Aspose.Cells for .NET. If you think it doesn't help or I'm unable to understand your question clearly then please share your further thoughts.
Disclosure: I work as a developer evangelist at Aspose.

Related

Power Query: How do I add a specific List/Vector as a Column

I have a simple problem. Let's assume I have the following
Now I want to add a specific vector or list for example (1,2,2,1) as an additional column and it should look like
When I add "Custom Column", how do I have to enter this list that it creates exactly this column?
It should not be a index column or a calculated column because I want to specify the values depending on another column manually. It seems like it is a very simple task, but I couldn't find any solutions yet! (I feel very stupid xD)
pls help
Thank you!
let Source = #table({"Column1"},{{"A"},{"B"},{"C"},{"D"}}),
Add = {{1,2,2,1}},
part1=Table.ToColumns(Source) & Add,
part2= Table.FromColumns(part1,Table.ColumnNames(Source)&{"Column2"})
in part2
or perhaps, if grabbing column from another table
let Source = #table({"Column1"},{{"A"},{"B"},{"C"},{"D"}}),
part1=Table.ToColumns(Source) & Table.ToColumns(Table.SelectColumns(SomeOtherTable,"Column9")),
part2 = Table.FromColumns(part1,Table.ColumnNames(Source)&{"Column2"})
in part2
Copy your Column1
Open the Enter Data / Create Table UI and paste Column1
Add your specific vector as Column2 and save the new table
Merge the new table into your original table using both Column1
Note that Column1 has to be a key column (unique values). If it's not, create a temporary index column and use that for merging.

Power Query / Power BI - How to move a cell value to a separate cell the easiest way?

I want to move a single value from column B to column A, how can I achieve it in the most simplest way in Power Query / Query Editor (Power BI)?
Please see attached images.
I know I might need to declare a variable so please enlighten me. By the way, I will delete row 1 afterwards, promote my headers, and rename column2 as PERIOD.
Thank you.
This might be along the lines of what you want to do.
If I start with this table named as Table1:
Then I click on the fx to the left of the formula bar:
And type = Table.InsertRows(Source, Table.RowCount(Source), {[Column2 = Source[KP20 rate]{0}, KP20 rate = null, Column4 = null]}) into the formula bar:
I used Table.InsertRows to create a new row in Table1. Source is the name of the latest state of Table1 after it is pulled into Power Query and before I do this step. So I actually use Source as the name of the table for this step instead of Table1. (Each applied step basically results in its own table. You probably know this already, but others may not.) So for this step I use Source as the table name in the Table.InsertRows statement. Then, since I want the new row to appear at the bottom of Source, I just enter the Table.RowCount of Source as the row number location for the new row. Then I enter each of the Columns' names and their values to be added. For Column2, I entered the value "Source[KP20 rate]{0}." Source[KP20 rate]{0} basically treats column KP20 rate as a list, where {0} serves as a pointer to the first item in the list. To target the second item in Source[KP20 rate] you would use Source[KP20 rate]{1}. You can see that I set the values for the other two columns (KP20 rate and Column4) to null.
The result:
Here's the M code in case you want to see it:
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
Custom1 = Table.InsertRows(Source, Table.RowCount(Source), {[Column2 = Source[KP20 rate]{0}, KP20 rate = null, Column4 = null]})
in
Custom1

Change value of a single cell in Power BI

I am trying to change the value of a single cell in power bi. When I go into the "data" view then right click and then go to "edit query" I have the option for "replace values". This works like a find and replace, the issue I am having is that if there are multiple cells with the same values they will all get changed. How can I just edit a single cell??
Unfortunately it is not as easy as just editing the cell. If you do not have an index/id column in your table then add one (there is a button on the toolbar to do this).
Then create a new custom column in M (PowerQuery / the data prep language of PowerBI), along the lines of:
NewColumn = IF ([Index] = "Index Number" and [YourColumn] = "<your value>")
THEN "The new text"
ELSE [YourOriginalColumn]
Rename or remove the original column and rename NewColumn to the original column name.

Filling a DataGridView ComboBox with values in C/C++

I'm creating a windows forms application for university. My data is stored in multible c arrays of abstract data types. Each array is displayed in it's own datagridview. This all works no problem.
Since the data is connected like in a database I added a new column to my parent table and set the ColumnType to DataGridViewComboBoxColumn. Now I need to fill this combobox with the data of a different array / datagridview.
I've already tried to add the options manually from my array as discribed in this question: Filling DataGridView ComboBox programatically in unbound mode?. Since I need to use C/C++ and this example uses vb the used functions are not availible. I can select my column dataGridView1->Columns[0] but the ->Items does not exist.
Is it possible to create a datasource from my arrays to link them directly? Or have I overlooked a way to add them manually from my array?
You don't provide any code, neither you describe well where did you ran into a problem. Assuming you're after unbound list to be filled into the DataGridViewComboBoxColumn, here is a simple example.
C#:
DataTable dt;
dt.Columns.Add("ID", typeof(Int32));
dt.Columns.Add("Category", typeof(string));
dt.Rows.Add({1, "Fruits"});
dt.Rows.Add({2, "Vegetables"});
dt.Rows.Add({3, "Trees"});
DataGridViewComboBoxColumn dgCol = this.DataGridView1.columns("Category");
dgCol.ValueMember = "ID";
dgCol.DisplayMember = "Category";
dgCol.DataSource = dt;
In this example, we create a DataTable, fill it with desired values and use it as a DataSource of the DataGridViewComboBoxColumn. Obviously, your values provided to this column in the DataGridView DataSource must match IDs in the list in new DataTable "dt".
You can convert an array or other data into DataTable or even use them directly as DataSource. It depends on what data you have. The principle will be very similar, though.
VB.NET:
Dim dt As DataTable
dt.Columns.Add("ID", GetType(Int32))
dt.Columns.Add("Category", GetType(String))
dt.Rows.Add({1, "Fruits"})
dt.Rows.Add({2, "Vegetables"})
dt.Rows.Add({3, "Trees"})
Dim dgCol As DataGridViewComboBoxColumn = Me.DataGridView1.columns("Category")
dgCol.ValueMember = "ID"
dgCol.DisplayMember = "Category"
dgCol.DataSource = dt

How to create a pygsheets worksheet without the default sheet1?

Is there a way to create a pygsheets worksheet without the default sheet1?
Looking through the documentation didn't help, but googling seems to indicate that it's possible to eliminate sheet1 in excel, so presumably, it should be possible in pygsheets.
sheet1 just refers to the first sheet which is automatically created when you creates the spreadsheet. It is automatically created by google sheets and you can't control it from any library. google sheets required you to have at-least one worksheet in a spreadsheet. So you cannot remove all the sheets.
But if you just want to remove the sheet named sheet1, you can do
sh.del_worksheet(sh.sheet1)
or if you wanna rename it , you can do
sh.sheet1.title="new_sheet"
Simply rename the default sheet1 to your desired worksheet name (my_sheet1)
sh = gc.create("my_google_sheet", parent_id="56uyjJcWghbtyuwA")
wks = sh.sheet1
# rename to your desired worksheet name
wks.title = ('my_sheet1')