简体   繁体   English

在LibreOffice Calc中将STRING重铸为VALUE

[英]Recasting a STRING into a VALUE in LibreOffice Calc

I have a Python class that does some currency conversion and string formatting of numbers. 我有一个Python类,可以进行一些货币换算和数字的字符串格式化。 It takes polymorphic input, but only spits out a stringified number. 它接受多态输入,但只吐出一个字符串化的数字。 I can push those stringified numbers up to a LibreOffice Calc in Python easy enough: 我可以很容易地将这些字符串化的数字推入Python中的LibreOffice Calc:

stringifiednumber = str("1.01")
cell_a1 = sheet1.getCellRange("A1")
cell_a1.String = stringifiednumber

This actually works nicely since the builtin currency formats in Calc work just fine with stringified numbers. 实际上这很好用,因为Calc中的内置货币格式可以很好地处理带数字的数字。

What doesn't work is formulas, or sort of doesn't work. 公式不起作用,或者某种形式不起作用。 Calling SUM(A1:A2) will not see the stringified A1. 调用SUM(A1:A2)将看不到字符串化的A1。 There is a workaround (forgive me it is late and I forget it exactly but it is similar to:) =SUMRECORD(VALUE(A1:A2)) . 有一个解决方法(原谅我来晚了,我完全忘记了,但它类似于:) =SUMRECORD(VALUE(A1:A2))

As I understand it, each cell has a memory location for a number, a string, and a formula. 据我了解,每个单元格都有一个用于存储数字,字符串和公式的存储位置。 The formula only acts on the VALUE memory location. 该公式仅作用于VALUE存储位置。

Through the spreadsheet UI, I can convert one cell type to another during a copy. 通过电子表格用户界面,我可以在复制期间将一种单元格类型转换为另一种。 To do that I just put the following formula in A2, and it converts STRING(A1) to VALUE( A2) : 为此,我只需将以下公式放在A2中,它将STRING(A1)转换为VALUE( A2)

# formula placed in A2

    =VALUE(A1)

but that only works by copying one cell to another. 但这只能通过将一个单元格复制到另一个单元格来实现。 Obviously there is an internal recasting function within the spreadsheet that is doing the conversion during the copy. 显然,电子表格中有一个内部重铸功能,可以在复制期间进行转换。

What I want to do, is write a stringified number to the spreadsheet (as above) and then call the spreadsheets native recasting function in place from Python, so that VALUE(A1) is recast from STRING(A1). 我想做的是向电子表格中写入一个字符串化的数字(如上所述),然后从Python调用电子表格本机重铸函数,以便从STRING(A1)重铸VALUE(A1)。

If I knew what the recasting function was I could just call it after every string write. 如果我知道什么是重铸函数,则可以在每次写入字符串后调用它。 This would make macros in the UI work like the user expects them to work. 这将使UI中的宏像用户期望的那样工作。

If your answer is: "do type conversion Python-side", I've already considered that, and it is not the solution I'm looking for. 如果您的答案是:“在Python端进行类型转换”,我已经考虑过了,这不是我要寻找的解决方案。

Based on your Title, multiply by 1 : 根据您的标题,乘以1

将文本“ 6”乘以<code> 1 </ code>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM