英文:
How do I replace parts of a formatted date with another value in Libreoffice Calc?
问题
我正在使用Ubuntu 22.04上的LibreOffice 7.5(目前最新版本)。
我有很多带有德国区域设置格式的日期值的单元格(如果这很重要的话),如
1.4.2023
4.4.2023
等等。
我想要替换所有这些单元格中的月份或年份为不同的(有效)数字。我该如何做到这一点?普通的搜索和替换可能不起作用,因为它扫描的是实际值,而不是格式化的日期。最好不需要事先计算要添加多少天、秒或其他内容以获得N年或N月的变化。
如果上述方法不可行,备选方案B:如何告诉LibreOffice快速(通过快捷键?)将特定年份自动填充到没有年份的单元格中,这样当我输入1.4.
(4月1日)时,它不会自动填充当前年份,而是将我之前指定的年份填入日期中?这应该至少在关闭LibreOffice之前保持,或在设置另一年之前保持。
英文:
I am using Libreoffiec 7.5 (currently the newest version) on Ubuntu 22.04.
I have a lot of cells with date values formatted with the German locale (if this matters), like
1.4.2023
4.4.2023
etc.
I want to replace the month, or the year, in all these cells with a different (valid) number. How do I do this? Plain search & replace does not work probably because it scans the actual value, which is a number, not the formatted date. Preferably without having to calculate beforehand how many days, or seconds, or whatever I would have to add to the date to end up with a shift of N years or months.
Plan B if the above is not possible: How do I tell Libreoffice quickly (by hotkey?) that it should autofill a specific year into cells which receive a date without year, so that when I type 1.4.
(April 1st) it doesn't autofill the current year, but my previously specified year into the date? This should persist at least until closing LibreOffice, or until setting another year.
答案1
得分: 1
代码部分不翻译,以下是翻译好的文本:
简短答案 对于两个问题都是否定的。
更多细节: 无论日期单元格的格式如何,查找和替换 操作都无法执行所需的操作(你是对的,这是由于日期的内部表示形式,从“基准日期”开始计算的天数,工具 - 选项 - LibreOffice Calc - 计算 - 日期部分);输入不完整的日期(日.月)总是指的当前年份。
完整答案。
关于转换现有数据的一般建议:总是分两个阶段进行转换 - 创建一个辅助范围,在其中使用公式或内置的Calc机制创建所需的数据,在仔细检查转换的正确性后,使用复制和粘贴特殊值来替代原始数据并清除辅助范围。
假设你在问题中展示的日期位于列A。
使用 Year(),Month() 和 Day() 函数获取原始日期的相应部分,使用 DATE() 函数生成具有必要修正的新日期。
例如,要获取原始日期后一年半的日期,你可以使用以下公式:
=DATE(YEAR(A2);MONTH(A2)+18;DAY(A2))
或
=DATE(YEAR(A2)+1;MONTH(A2)+6;DAY(A2))
当然,你可以替换DATE()函数的任何参数为固定值。例如,=DATE(1827;MONTH(A2);DAY(A2))
将返回A2单元格中的日和月,但为1827年(这样你可以实施你描述的计划B)。
除了这里描述的四个函数,Calc 还包含一组相当大的用于处理日期的函数。你可以在 函数向导 的日期部分中查看它们:
或者你可以在 相应的帮助部分 中阅读每个这些函数的详细描述:
英文:
The short answer to both questions is No way.
More details: regardless of the formatting of the cell with the date, the Find & Replace operation will not perform the required operation (you are right, this is due to the internal representation of the date as the number of days counted from the "base date", Tools - Options - LibreOffice Calc - Calculate - Date section); entering an incomplete date (day.month) always refers to the current year.
Full answer.
General recommendation for converting existing data: always convert in two stages - create an auxiliary range in which, using formulas or built-in Calc mechanisms, create the necessary data, after carefully checking the correctness of the conversion, use Copy and Paste Special - Values in place of the original data and clear auxiliary range.
Let's assume that the dates you showed in your question as an example are in column A.
Use the Year(), Month(), and Day() functions to get the corresponding parts of the original date, use the DATE() function to generate a new date with the necessary corrections.
For example, to get a date one and a half years after the original, you can use the formula
=DATE(YEAR(A2);MONTH(A2)+18;DAY(A2))
or
=DATE(YEAR(A2)+1;MONTH(A2)+6;DAY(A2))
Of course, instead of any of the parameters of the DATE() function, you can substitute a fixed value. For example, =DATE(1827;MONTH(A2);DAY(A2))
will return with the day and month from cell A2, but for 1827 (So you can implement the Plan B you described).
In addition to the four functions described here, Calc contains a rather large set of functions for working with dates. You can see them in the Date section of the Function Wizard:
or you can read a detailed description of each of these functions in the corresponding Help section
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论