Data extraction from excel with operators is unable to store values - excel

I have a Excel file with two columns. One has a name other has the corresponding mass to it. I have used the corresponding lines to read it and find the position of the name. But when I am trying to find the mass to the corresponding name as shown below it is not able to store it in the memory. In the Excel file, I have the mass values as 1.989*10^30. This seems to affect the code as the same code works fine when the cells in the excel has just numeric values.
majbod = 'Sun';
minbod = 'Earth';
majbodin = readtable("Major_and_Minor_Bodies.xlsx","Sheet",1);
minbodin = readtable("Major_and_Minor_Bodies.xlsx","Sheet",2);
MAJORBODY = table2array(majbodin(:,"Major_Body"));
MINORBODY = table2array(minbodin(:,"Minor_Body"));
mmaj = table2array(majbodin(:,"Mass"));
mmin = table2array(minbodin(:,"Mass"));
selected_majbody = find(strcmp(MAJORBODY,majbod));
selected_minbody = find(strcmp(MINORBODY,minbod));
M = mmaj(selected_majbody);
m = mmin(selected_minbody);
disp([M ;m])
Is there a better way to write the code compared to the way which I wrote?
Thanks.

Excel does it's best to figure out what kind of data is in each cell. Since your data has something besides just numbers, Excel treats it like a string. You have a couple of options for getting around that:
If you put an equals sign in front of it, it will treat it like an equation, and calculate the value of 1.989*10^3 for you. this will be a number.
Since scientific notation is so common, programmers have created a shortcut for representing it. They often use the character 'E' where you use "*10^". This means that if you type "1.989E30", excel will recognize that as a number.
If keeping the current string format is very important, you could probably modify the string during extraction - replace '*10^' with E, and then whatever language you are using will have a string to number parser you can use.
If the real problem is that the real numbers are just too long in Excel, you can always format the cell that they are in. (right click the cell, select format cells, then select scientific.)
Good luck

Related

Making a vector out of excel columns using python

everyone...
I just started on python a couple of days ago because I require to handle some excel data in order to automatically update the data of certain cells from one file into another.
However, I'm kind of stuck since I have barely programmed before, and it's my first time using python as well, but my job required me to find a solution and I'm trying to make it work even though it's not my field of expertise.
I used the "xlrd library", imported my file and managed to print the columns I'm needing... However, I can't find a way to put those columns into a matrix in order to handle the data like this:
Matrix =[DataColumnA DataColumnG DataColumnH] in the size [nrows x 3]
As for now, I have 3 different outputs for the 3 different columns I need, but I'm trying to join them together into one big matrix.
So far my code looks like this:
import xlrd
workbook = xlrd.open_workbook("190219_serviciosWRAmanualV5.xls");
worksheet = workbook.sheet_by_name("ServiciosDWDM");
workbook2 = xlrd.open_workbook("Potencia2.xlsx");
worksheet2 = workbook2.sheet_by_name("Hoja1");
filas = worksheet.nrows
filas2 = worksheet2.nrows
columnas = worksheet.ncols
for row in range (2, filas):
Equipo_A = worksheet.cell(row,12).value
Client_A = worksheet.cell(row,13).value
Line_A = worksheet.cell(row, 14).value
print (Equipo_A, Line_A, Client_A)
So I have only gotten, as mentioned above, the data in the columns which is what I'm printing which you can see.
What I'm trying to do, or the main thing I need to do is to read the cell of the first row in Column A and look for it in the other excel file... if the names match, I would have to validate that for the same row (in file 1) the data in both the ColumnG and ColumnH is the same as the data in the second file.
If they match I would have to update Column J in the first file with the data from the second file.
My other approach is to retrieve the value of the cell in ColumnA and look for it in the column A of the second file, then I would make an if conditional to see if ColumnsG and H are equal to Column C of 2nd file and so on...
The thing here is, I have no idea how to pin point the position of the cell and extract the data to make the conditional for this second approach.
I'm not sure if by making that matrix my approach is okay or if the second way is better, so any suggestion would be absolutely appreciated.
Thank you in advance!

Convert value of money

I can't seem to find this or I am not searching for the correct term. I am simply trying to change money to long string. The data being pulled to D cell has it as $7.5K for example. I would like to remove the dollar symbol and write the decimal to long form so change $7.5K to 7500.
A few notes:
-ALL the data is above $1K so nothing will be below that amount.
-money has no ONEs or TENs value so these are always 00.
Not sure if I can automate this or make it easier to convert these values using the Find/Replace.
If the true /underlying value is a number, simply reformat as Number with no decimal places. If a text string either:
Replace K by *1000 and then $ by = and format to suit
or, as suggested by #Ken White, apply a formula such as:
=1000*SUBSTITUTE(SUBSTITUTE(A1,"$",""),"K","")
again with suitable formatting.

Apache POI : How to format numeric cell values

I am using Apache POI 3.9 for XLS/XLSX file processing.
In the XLS sheet, there is a column with numeric value like "3000053406".
When I read it with POI with..
cell.getNumericCellValue()
It gives me value like "3.00E+08". This create huge problem in my application.
How can I set the number formatting while reading data in Apcahe POI ?
There is a way that I know is to set the column as "text" type. But I want to know if there is any other way at Apache POI side while reading the data. OR can we format it by using simple java DecimalFormatter ?
This one comes up very often....
Picking one of my past answers to an almost identical question
What you want to do is use the DataFormatter class. You pass this a cell, and it does its best to return you a string containing what Excel would show you for that cell. If you pass it a string cell, you'll get the string back. If you pass it a numeric cell with formatting rules applied, it will format the number based on them and give you the string back.
For your case, I'd assume that the numeric cells have an integer formatting rule applied to them. If you ask DataFormatter to format those cells, it'll give you back a string with the integer string in it.
Problem can be strictly Java-related, not POI related, too.
Since your call returns a double,
double val = cell.getNumericCellValue();
You may want to get this
DecimalFormat df = new DecimalFormat("#");
int fractionalDigits = 2; // say 2
df.setMaximumFractionDigits(fractionalDigits);
double val = df.format(val);
Creating a BigDecimal with the double value from the numeric cell and then using the
BigDecimal.toPlainString()
function to convert it to a plain string and then storing it back to the same cell after erasing the value solved the whole problem of exponential representation of numeric values.
The below code solved the issue for me.
Double dnum = cellContent.getNumericCellValue();
BigDecimal bd = new BigDecimal(dnum);
System.out.println(bd.toPlainString());
cellContent.setBlank();
cellContent.setCellValue(bd.toPlainString());
System.out.println(cellContent.getStringCellValue());
long varA = new Double(cellB1.getNumericCellValue()).longValue();
This will bring the exact value in variable varA.

Excel turning my numbers to floats

I have a bit of ASP.NET code that exports data in a datagrid into Excel but I noticed that it messes up a particular field when exporting.
E.g. I have the value of something like 89234010000725515875 in a column in the datagrid but when exported, it turns it into 89234+19.
Is there any Excel formatting that will bring back my original number? Thanks.
Excel isn't really messing up the field. Two things are happening:
Excel formats large numbers in scientific notation. So "89234010000725515875" becomes "8.9234E+19" or "8.9234 x 10 ^ 19".
The size of the number "89234010000725515875" exceeds the precision in which Excel uses to store values. Excel stores your number as "89234010000725500000" so you're losing the last five digits.
Depending on your needs you can do one of two things.
Your first option is to change the formatting from "General" to "0" (Number with zero decimal places.) This will give you "89234010000725500000" so you will have lost precision but you will be able to perform calculcations on the number.
The second option is to format the cell as text "#" or to paste your field with an apostrophe at the beginning of the line to force the value to be text. You'll get all of the digits but you won't be able to do calculations of the value.
I hope this helps.
You can add a space to the field, then when you export it to Excel, it's considered as string:
lblTest.Text = DTInfo.Rows(0).Item("Test") & " "
Good luck.
Below is the C# source code to do this with SpreadsheetGear for .NET. Since the SpreadsheetGear API is similar to Excel's API, you should be able to easily adapt this code to Excel's API to get the same result.
You can download a free trial here if you want to try it yourself.
Disclaimer: I own SpreadsheetGear LLC
using System;
using SpreadsheetGear;
namespace Program
{
class Program
{
static void Main(string[] args)
{
// Create a new workbook and get a reference to A1.
IWorkbook workbook = Factory.GetWorkbook();
IWorksheet worksheet = workbook.Worksheets[0];
IRange a1 = worksheet.Cells["A1"];
// Format A1 as Text using the "#" format so that the text
// will not be converted to a number, and put the text in A1.
a1.NumberFormat = "#";
a1.Value = "89234010000725515875";
// Show that the formatted value is
Console.WriteLine("FormattedValue={0}, Raw Value={1}", a1.Text, a1.Value);
// Save the workbook.
workbook.SaveAs(#"c:\tmp\Text.xls", FileFormat.Excel8);
workbook.SaveAs(#"c:\tmp\Text.xlsx", FileFormat.OpenXMLWorkbook);
}
}
}

Multiply numbers in Excel or LibreOffice cell contents by a constant when they are mixed with text?

I have a long series of cells written like this (example text):
Example Number (3502, 456)
How would I multiply the numbers by 4 without having to delete the text?
I also have cells in the format [sic below]:
Example Number (3502,456) (4560,250) (2345,223)
et cetera, there are on average ten parentheses per text string.
Occasionally, the text might also be only one word long, e.g.
Example (3205, 456)
or
Example (3205,456) (4560,250) (2345,223)
et cetera.
(all above is [sic]).
As a sort of newbie to Excel (well, really Libre Office Calc but it's essentially the same), how would I do this? I don't want to go through and manually multiply all the numbers myself. The number I want to multiply by is 4. I've tried just running a find-and-replace to replace all ,'s and )'s with *4's, but the program I need these numbers for can't evaluate expressions, it needs single numbers.
There are some 110+ items on each list I need to change, and just one math error on any of the three lists (!) and the program won't run correctly (I'm resizing an image, and the points I plotted on the image didn't scale up with it). I don't want to risk it.
It should be possible to do this with a macro but unless I'm mistaken LibreOffice macro code is quite different from Excel VBA.
However if you can afford to use several columns of your spreadsheet to figure the values out, you can do so using formulae. If cell A1 contains
Example Number (3502,456) (4560,250) (2345,223)
and B1 contains
=MID(A1,FIND("(",A1)+1,9999)
then this formula will return the 3502 as a number:
=NUMBERVALUE(LEFT(B1,FIND(",",B1)-1))
(9999 is chosen to be much larger than the likely length of any line, so the MID function will always return the whole of the rest of the text after the search character).
You should be able to combine MID and FIND functions in further cells to isolate the other numbers, assuming these are always found in the format (xxx,yyy) as per your example. Then you can use a final formula to rebuild the string from the multiplied numbers:
="Example Number (" & 4*C1 & "," & 4*E1 & ")"
and so on.
If your data has a variable number of numbers to find, some of your FIND functions may return a #VALUE error. You may need to use an IF function to exclude these, for example:
=IF(ISERROR(G1),"",G1)
would return the value of G1 if it contains data, but blank if it contains an error.
Here is a Python LibreOffice macro that does what you want. It assumes all of the values are in column A, and it writes the results to column B.
import re
def do_calculations():
document = XSCRIPTCONTEXT.getDocument()
sheet = document.getSheets().getByIndex(0)
cellrange = sheet.getCellRangeByName("A1:A10000")
row_tuples = cellrange.getDataArray()
row = 1
for row_tuple in row_tuples:
if row_tuple:
row = output_values(row, row_tuple[0], sheet)
def output_values(row, pairs_string, sheet):
"""Multiply pairs of values by 4 and output each pair to B column.
:param row: the row number in the B column
:param pairs_string: a string like "Example Number (123, 456) (789, 1011)"
:param sheet: the current spreadsheet
Returns the next row number in the B column.
"""
pairs = re.findall(r'\([^)]+\)', pairs_string)
for pair in pairs:
match_obj = re.match(r'\((\d+),\s*(\d+)\)', pair)
x, y = match_obj.groups()
result = "(%d,%d)" % (int(x) * 4, int(y) * 4)
cell = sheet.getCellRangeByName("B" + str(row))
cell.setString(result)
row += 1
return row
# Functions that can be called from Tools -> Macros -> Run Macro.
g_exportedScripts = do_calculations,
Save the code to a text file, for example calc_multiply_numbers.py. Put it in Scripts/python in your LibreOffice user directory. On my Windows system it is C:\Users\JimStandard\AppData\Roaming\LibreOffice\4\user\Scripts\python. If the python directory doesn't exist yet, create it.
To run it, open the spreadsheet and go to Tools -> Macros -> Run Macro. Under My Macros, click calc_multiply_numbers and then press the Run button.
EDIT:
I don't think you need to worry about the JRE error. On my system I can uncheck "Use a Java runtime environment" in Tools -> Options -> LibreOffice -> Advanced, and it still works. I just click "No" when it asks if I want to enable the use of a JRE now, and then it runs my python macro.
The reason it is not showing up under My Macros is because python is not able to interpret the file correctly. To find the error, test it with python using the following steps (assuming Windows):
Open a command prompt, for example by pressing Win, typing cmd, and clicking "Command Prompt" from the start menu.
Type cd "path-to-libreoffice/program". On my 64-bit system this is cd "C:\Program Files (x86)\LibreOffice 5\program" I use the normal Windows File Explorer to find the exact path.
Type "python.exe python-script". On my system it is python.exe "C:\Users\JimStandard\AppData\Roaming\LibreOffice\4\user\Scripts\python\calc_multiply_numbers.py"
The python interpreter will give an error message about the problem. If you are not able to figure out the message, write it in the comments below and I will help you.

Resources