chore: do clear
This commit is contained in:
parent
9852d06e15
commit
ca9bb2771c
340
BookmarkOP.py
340
BookmarkOP.py
@ -1,32 +1,4 @@
|
|||||||
import json
|
import json
|
||||||
import time
|
|
||||||
|
|
||||||
def ReplaceOne(bookmark_obj_collection, bookmark_name, new_value):
|
|
||||||
"""
|
|
||||||
替换一个书签
|
|
||||||
Args:
|
|
||||||
bookmark_obj_collection: 书签对象集合
|
|
||||||
bookmark_name: 书签名称
|
|
||||||
new_value: 新值
|
|
||||||
"""
|
|
||||||
|
|
||||||
if bookmark_obj_collection.hasByName(bookmark_name):
|
|
||||||
bookmark = bookmark_obj_collection.getByName(bookmark_name)
|
|
||||||
anchor = bookmark.getAnchor()
|
|
||||||
anchor.setString(new_value)
|
|
||||||
|
|
||||||
|
|
||||||
def ReplaceAll(bookmark_obj_collection, bookmark_name_value_map):
|
|
||||||
"""
|
|
||||||
替换所有书签
|
|
||||||
Args:
|
|
||||||
bookmark_obj_collection: 书签对象集合
|
|
||||||
bookmark_name_value_map: 书签名称和新值的映射
|
|
||||||
"""
|
|
||||||
|
|
||||||
for bookmark_name, new_value in bookmark_name_value_map.items():
|
|
||||||
ReplaceOne(bookmark_obj_collection, bookmark_name, new_value)
|
|
||||||
|
|
||||||
|
|
||||||
def Replace(bookmark_name_value_map):
|
def Replace(bookmark_name_value_map):
|
||||||
"""
|
"""
|
||||||
@ -37,7 +9,11 @@ def Replace(bookmark_name_value_map):
|
|||||||
|
|
||||||
doc = XSCRIPTCONTEXT.getDocument()
|
doc = XSCRIPTCONTEXT.getDocument()
|
||||||
bookmarks = doc.getBookmarks()
|
bookmarks = doc.getBookmarks()
|
||||||
ReplaceAll(bookmarks, bookmark_name_value_map)
|
for bookmark_name, new_value in bookmark_name_value_map.items():
|
||||||
|
if bookmarks.hasByName(bookmark_name):
|
||||||
|
bookmark = bookmarks.getByName(bookmark_name)
|
||||||
|
anchor = bookmark.getAnchor()
|
||||||
|
anchor.setString(new_value)
|
||||||
|
|
||||||
|
|
||||||
def ReplaceWithJSON(json_str):
|
def ReplaceWithJSON(json_str):
|
||||||
@ -56,7 +32,7 @@ def DebugCallReplace():
|
|||||||
调试调用
|
调试调用
|
||||||
"""
|
"""
|
||||||
|
|
||||||
Replace({"bb1": "P1", "bb2": "P2", "bb3": "P3", "bb4": "P4"})
|
Replace({"合同编号_A": "11111", "合同名称_A": "11111"})
|
||||||
|
|
||||||
|
|
||||||
def DebugCallReplaceList():
|
def DebugCallReplaceList():
|
||||||
@ -66,6 +42,7 @@ def DebugCallReplaceList():
|
|||||||
|
|
||||||
Replace({"list": "1\r2\r3\r4"})
|
Replace({"list": "1\r2\r3\r4"})
|
||||||
|
|
||||||
|
|
||||||
def QueryAll():
|
def QueryAll():
|
||||||
"""
|
"""
|
||||||
查询所有书签,表格中的书签获取对应表格index
|
查询所有书签,表格中的书签获取对应表格index
|
||||||
@ -85,27 +62,14 @@ def QueryAll():
|
|||||||
doc = XSCRIPTCONTEXT.getDocument()
|
doc = XSCRIPTCONTEXT.getDocument()
|
||||||
bookmarks = doc.getBookmarks()
|
bookmarks = doc.getBookmarks()
|
||||||
bookmark_names = bookmarks.getElementNames()
|
bookmark_names = bookmarks.getElementNames()
|
||||||
filtered_bookmarks = [bk_name for bk_name in bookmark_names if not bk_name.startswith('_') and ':' not in bk_name]
|
filtered_bookmarks = [
|
||||||
|
bk_name
|
||||||
|
for bk_name in bookmark_names
|
||||||
|
if not bk_name.startswith("_") and ":" not in bk_name
|
||||||
|
]
|
||||||
result = {"text": [], "table": []}
|
result = {"text": [], "table": []}
|
||||||
|
|
||||||
# tables = doc.getTextTables()
|
|
||||||
# bookmark_in_table_position = QueryBookmarkPositionInTable(tables, bookmarks)
|
|
||||||
|
|
||||||
existing_table_indices = {}
|
|
||||||
|
|
||||||
for bk_name in filtered_bookmarks:
|
for bk_name in filtered_bookmarks:
|
||||||
# if bk_name in bookmark_in_table_position:
|
|
||||||
# table_index = bookmark_in_table_position[bk_name]["tableIndex"]
|
|
||||||
# if table_index in existing_table_indices:
|
|
||||||
# index = existing_table_indices[table_index]
|
|
||||||
# result["table"][index]["bookmark"].append(bk_name)
|
|
||||||
# else:
|
|
||||||
# new_entry = {"tableIndex": table_index, "bookmark": [bk_name]}
|
|
||||||
# result["table"].append(new_entry)
|
|
||||||
# existing_table_indices[table_index] = (
|
|
||||||
# len(result["table"]) - 1
|
|
||||||
# ) # 更新索引记录
|
|
||||||
# else:
|
|
||||||
result["text"].append(bk_name)
|
result["text"].append(bk_name)
|
||||||
|
|
||||||
return result
|
return result
|
||||||
@ -114,6 +78,7 @@ def QueryAll():
|
|||||||
def QueryAllWithJSON():
|
def QueryAllWithJSON():
|
||||||
return returnWithJSON(QueryAll())
|
return returnWithJSON(QueryAll())
|
||||||
|
|
||||||
|
|
||||||
def QueryBookmarkPositionInTable(tables, bookmarks):
|
def QueryBookmarkPositionInTable(tables, bookmarks):
|
||||||
"""
|
"""
|
||||||
查询书签在表格中的位置
|
查询书签在表格中的位置
|
||||||
@ -147,29 +112,6 @@ def QueryBookmarkPositionInTable(tables, bookmarks):
|
|||||||
|
|
||||||
return bookmark_position
|
return bookmark_position
|
||||||
|
|
||||||
|
|
||||||
def CreateRow(table, row_count):
|
|
||||||
"""
|
|
||||||
创建表格行
|
|
||||||
"""
|
|
||||||
table.getRows().insertByIndex(table.getRows().getCount(), row_count)
|
|
||||||
|
|
||||||
|
|
||||||
def FindTableIndex(table_bookmarks, location_bookmark_name):
|
|
||||||
"""
|
|
||||||
找到表格索引
|
|
||||||
"""
|
|
||||||
handle_table_index = -1
|
|
||||||
for table_info in table_bookmarks:
|
|
||||||
if location_bookmark_name in table_info["bookmark"]:
|
|
||||||
handle_table_index = table_info["tableIndex"]
|
|
||||||
break
|
|
||||||
|
|
||||||
if handle_table_index == -1:
|
|
||||||
raise ValueError(f"未找到书签 {location_bookmark_name} 对应的表格")
|
|
||||||
return handle_table_index
|
|
||||||
|
|
||||||
|
|
||||||
def InsertRowWithJSON(json_str):
|
def InsertRowWithJSON(json_str):
|
||||||
"""
|
"""
|
||||||
插入行
|
插入行
|
||||||
@ -194,7 +136,9 @@ def InsertRow(location_bookmark_name, data, start_row_index=-1):
|
|||||||
if location_bookmark_name not in bookmark_in_table_position:
|
if location_bookmark_name not in bookmark_in_table_position:
|
||||||
raise ValueError(f"未找到书签 {location_bookmark_name} 对应的表格")
|
raise ValueError(f"未找到书签 {location_bookmark_name} 对应的表格")
|
||||||
|
|
||||||
handle_table_index = bookmark_in_table_position[location_bookmark_name]["tableIndex"]
|
handle_table_index = bookmark_in_table_position[location_bookmark_name][
|
||||||
|
"tableIndex"
|
||||||
|
]
|
||||||
|
|
||||||
try:
|
try:
|
||||||
handle_table = tables.getByIndex(handle_table_index)
|
handle_table = tables.getByIndex(handle_table_index)
|
||||||
@ -213,27 +157,27 @@ def InsertRow(location_bookmark_name, data, start_row_index=-1):
|
|||||||
try:
|
try:
|
||||||
row_count = handle_table.getRows().getCount()
|
row_count = handle_table.getRows().getCount()
|
||||||
rows_to_insert = len(data)
|
rows_to_insert = len(data)
|
||||||
|
|
||||||
if rows_to_insert > 0:
|
if rows_to_insert > 0:
|
||||||
# 确定插入位置
|
# 确定插入位置
|
||||||
if start_row_index != -1 and 0 <= start_row_index < row_count:
|
if start_row_index != -1 and 0 <= start_row_index < row_count:
|
||||||
insert_pos = start_row_index + 1 # 在指定行下方插入
|
insert_pos = start_row_index + 1 # 在指定行下方插入
|
||||||
else:
|
else:
|
||||||
insert_pos = row_count # 插入到表格末尾
|
insert_pos = row_count # 插入到表格末尾
|
||||||
|
|
||||||
# 批量插入所有新行
|
# 批量插入所有新行
|
||||||
handle_table.getRows().insertByIndex(insert_pos, rows_to_insert)
|
handle_table.getRows().insertByIndex(insert_pos, rows_to_insert)
|
||||||
|
|
||||||
# 填充数据到新插入的行
|
# 填充数据到新插入的行
|
||||||
for data_row_idx, row_data in enumerate(data):
|
for data_row_idx, row_data in enumerate(data):
|
||||||
target_row = insert_pos + data_row_idx # 计算目标行索引
|
target_row = insert_pos + data_row_idx # 计算目标行索引
|
||||||
for col_idx, cell_value in enumerate(row_data):
|
for col_idx, cell_value in enumerate(row_data):
|
||||||
cell = handle_table.getCellByPosition(col_idx, target_row)
|
cell = handle_table.getCellByPosition(col_idx, target_row)
|
||||||
cell.setString(str(cell_value))
|
cell.setString(str(cell_value))
|
||||||
|
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise RuntimeError(f"插入行时发生错误: {str(e)}")
|
raise RuntimeError(f"err: {str(e)}")
|
||||||
|
|
||||||
|
|
||||||
def BatchInsertRowWithContentControl(data_array):
|
def BatchInsertRowWithContentControl(data_array):
|
||||||
"""
|
"""
|
||||||
@ -245,13 +189,15 @@ def BatchInsertRowWithContentControl(data_array):
|
|||||||
bookmark_in_table_position = QueryBookmarkPositionInTable(tables, bookmarks)
|
bookmark_in_table_position = QueryBookmarkPositionInTable(tables, bookmarks)
|
||||||
|
|
||||||
for arr_obj in data_array:
|
for arr_obj in data_array:
|
||||||
location_bookmark_name = arr_obj.get('location_bookmark_name')
|
location_bookmark_name = arr_obj.get("location_bookmark_name")
|
||||||
data = arr_obj.get('data')
|
data = arr_obj.get("data")
|
||||||
start_row_index = arr_obj.get('start_row_index', -1) # 默认值为 -1
|
start_row_index = arr_obj.get("start_row_index", -1) # 默认值为 -1
|
||||||
if location_bookmark_name not in bookmark_in_table_position:
|
if location_bookmark_name not in bookmark_in_table_position:
|
||||||
raise ValueError(f"未找到书签 {location_bookmark_name} 对应的表格")
|
raise ValueError(f"未找到书签 {location_bookmark_name} 对应的表格")
|
||||||
|
|
||||||
handle_table_index = bookmark_in_table_position[location_bookmark_name]["tableIndex"]
|
handle_table_index = bookmark_in_table_position[location_bookmark_name][
|
||||||
|
"tableIndex"
|
||||||
|
]
|
||||||
|
|
||||||
try:
|
try:
|
||||||
handle_table = tables.getByIndex(handle_table_index)
|
handle_table = tables.getByIndex(handle_table_index)
|
||||||
@ -270,32 +216,38 @@ def BatchInsertRowWithContentControl(data_array):
|
|||||||
try:
|
try:
|
||||||
row_count = handle_table.getRows().getCount()
|
row_count = handle_table.getRows().getCount()
|
||||||
rows_to_insert = len(data)
|
rows_to_insert = len(data)
|
||||||
|
|
||||||
if rows_to_insert > 0:
|
if rows_to_insert > 0:
|
||||||
# 确定插入位置
|
# 确定插入位置
|
||||||
if start_row_index != -1 and 0 <= start_row_index < row_count:
|
if start_row_index != -1 and 0 <= start_row_index < row_count:
|
||||||
insert_pos = start_row_index + 1 # 在指定行下方插入
|
insert_pos = start_row_index + 1 # 在指定行下方插入
|
||||||
else:
|
else:
|
||||||
insert_pos = row_count # 插入到表格末尾
|
insert_pos = row_count # 插入到表格末尾
|
||||||
|
|
||||||
# 批量插入所有新行
|
# 批量插入所有新行
|
||||||
handle_table.getRows().insertByIndex(insert_pos, rows_to_insert)
|
handle_table.getRows().insertByIndex(insert_pos, rows_to_insert)
|
||||||
|
|
||||||
# 填充数据到新插入的行
|
# 填充数据到新插入的行
|
||||||
for data_row_idx, row_data in enumerate(data):
|
for data_row_idx, row_data in enumerate(data):
|
||||||
target_row = insert_pos + data_row_idx # 计算目标行索引
|
target_row = insert_pos + data_row_idx # 计算目标行索引
|
||||||
for col_idx, cell_value in enumerate(row_data):
|
for col_idx, cell_value in enumerate(row_data):
|
||||||
cell = handle_table.getCellByPosition(col_idx, target_row)
|
cell = handle_table.getCellByPosition(col_idx, target_row)
|
||||||
# cell.setString(str(cell_value))
|
|
||||||
# 创建文本控件并赋值 -----------------------------
|
|
||||||
cell_text = cell.getText()
|
cell_text = cell.getText()
|
||||||
cell_text.setString("") # 清空原有内容
|
|
||||||
|
|
||||||
text_control = doc.createInstance("com.sun.star.text.TextField.Input")
|
# === 清空单元格 ===
|
||||||
text_control.setPropertyValue("Content", str(cell_value))
|
cell_text.setString("")
|
||||||
|
|
||||||
|
# === 创建内容控件 ===
|
||||||
|
content_control = doc.createInstance("com.sun.star.text.ContentControl")
|
||||||
|
|
||||||
|
# === 插入控件到单元格 ===
|
||||||
cursor = cell_text.createTextCursor()
|
cursor = cell_text.createTextCursor()
|
||||||
cell_text.insertTextContent(cursor, text_control, False)
|
cell_text.insertTextContent(cursor, content_control, False)
|
||||||
|
|
||||||
|
# === 设置控件内容 ===
|
||||||
|
cc_text = content_control.getText()
|
||||||
|
cc_cursor = cc_text.createTextCursor()
|
||||||
|
cc_cursor.setString(str(cell_value))
|
||||||
|
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@ -314,13 +266,15 @@ def BatchInsertRow(data_array):
|
|||||||
bookmark_in_table_position = QueryBookmarkPositionInTable(tables, bookmarks)
|
bookmark_in_table_position = QueryBookmarkPositionInTable(tables, bookmarks)
|
||||||
|
|
||||||
for arr_obj in data_array:
|
for arr_obj in data_array:
|
||||||
location_bookmark_name = arr_obj.get('location_bookmark_name')
|
location_bookmark_name = arr_obj.get("location_bookmark_name")
|
||||||
data = arr_obj.get('data')
|
data = arr_obj.get("data")
|
||||||
start_row_index = arr_obj.get('start_row_index', -1) # 默认值为 -1
|
start_row_index = arr_obj.get("start_row_index", -1) # 默认值为 -1
|
||||||
if location_bookmark_name not in bookmark_in_table_position:
|
if location_bookmark_name not in bookmark_in_table_position:
|
||||||
raise ValueError(f"未找到书签 {location_bookmark_name} 对应的表格")
|
raise ValueError(f"未找到书签 {location_bookmark_name} 对应的表格")
|
||||||
|
|
||||||
handle_table_index = bookmark_in_table_position[location_bookmark_name]["tableIndex"]
|
handle_table_index = bookmark_in_table_position[location_bookmark_name][
|
||||||
|
"tableIndex"
|
||||||
|
]
|
||||||
|
|
||||||
try:
|
try:
|
||||||
handle_table = tables.getByIndex(handle_table_index)
|
handle_table = tables.getByIndex(handle_table_index)
|
||||||
@ -339,50 +293,38 @@ def BatchInsertRow(data_array):
|
|||||||
try:
|
try:
|
||||||
row_count = handle_table.getRows().getCount()
|
row_count = handle_table.getRows().getCount()
|
||||||
rows_to_insert = len(data)
|
rows_to_insert = len(data)
|
||||||
|
|
||||||
if rows_to_insert > 0:
|
if rows_to_insert > 0:
|
||||||
# 确定插入位置
|
# 确定插入位置
|
||||||
if start_row_index != -1 and 0 <= start_row_index < row_count:
|
if start_row_index != -1 and 0 <= start_row_index < row_count:
|
||||||
insert_pos = start_row_index + 1 # 在指定行下方插入
|
insert_pos = start_row_index + 1 # 在指定行下方插入
|
||||||
else:
|
else:
|
||||||
insert_pos = row_count # 插入到表格末尾
|
insert_pos = row_count # 插入到表格末尾
|
||||||
|
|
||||||
# 批量插入所有新行
|
# 批量插入所有新行
|
||||||
handle_table.getRows().insertByIndex(insert_pos, rows_to_insert)
|
handle_table.getRows().insertByIndex(insert_pos, rows_to_insert)
|
||||||
|
|
||||||
# 填充数据到新插入的行
|
# 填充数据到新插入的行
|
||||||
for data_row_idx, row_data in enumerate(data):
|
for data_row_idx, row_data in enumerate(data):
|
||||||
target_row = insert_pos + data_row_idx # 计算目标行索引
|
target_row = insert_pos + data_row_idx # 计算目标行索引
|
||||||
for col_idx, cell_value in enumerate(row_data):
|
for col_idx, cell_value in enumerate(row_data):
|
||||||
cell = handle_table.getCellByPosition(col_idx, target_row)
|
cell = handle_table.getCellByPosition(col_idx, target_row)
|
||||||
cell.setString(str(cell_value))
|
cell.setString(str(cell_value))
|
||||||
|
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise RuntimeError(f"插入行时发生错误: {str(e)}")
|
raise RuntimeError(f"插入行时发生错误: {str(e)}")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def InsertRowWithJSONDefineCol(json_str):
|
|
||||||
"""
|
|
||||||
插入行
|
|
||||||
"""
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def DebugCallInsertRow():
|
|
||||||
"""
|
|
||||||
调试调用
|
|
||||||
"""
|
|
||||||
InsertRow("t1", [["1", "2"], ["4", "5"]], 0)
|
|
||||||
|
|
||||||
|
|
||||||
def DeleteRowWithJSON(json_str):
|
def DeleteRowWithJSON(json_str):
|
||||||
"""
|
"""
|
||||||
删除行
|
删除行
|
||||||
"""
|
"""
|
||||||
data = json.loads(json_str)
|
data = json.loads(json_str)
|
||||||
DeleteRow(data["location_bookmark_name"], data["start_row_index"], data["delete_row_count"])
|
DeleteRow(
|
||||||
|
data["location_bookmark_name"],
|
||||||
|
data["start_row_index"],
|
||||||
|
data["delete_row_count"],
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def DeleteRow(location_bookmark_name, start_row_index, delete_row_count=-1):
|
def DeleteRow(location_bookmark_name, start_row_index, delete_row_count=-1):
|
||||||
"""
|
"""
|
||||||
@ -393,10 +335,6 @@ def DeleteRow(location_bookmark_name, start_row_index, delete_row_count=-1):
|
|||||||
start_row_index: 起始行位置(删除行包含本行)
|
start_row_index: 起始行位置(删除行包含本行)
|
||||||
delete_row_count: 删除的行数
|
delete_row_count: 删除的行数
|
||||||
"""
|
"""
|
||||||
# bookmark_info = QueryAll()
|
|
||||||
# table_bookmarks = bookmark_info["table"]
|
|
||||||
# handle_table_index = FindTableIndex(table_bookmarks, location_bookmark_name)
|
|
||||||
|
|
||||||
doc = XSCRIPTCONTEXT.getDocument()
|
doc = XSCRIPTCONTEXT.getDocument()
|
||||||
tables = doc.getTextTables()
|
tables = doc.getTextTables()
|
||||||
bookmarks = doc.getBookmarks()
|
bookmarks = doc.getBookmarks()
|
||||||
@ -404,8 +342,9 @@ def DeleteRow(location_bookmark_name, start_row_index, delete_row_count=-1):
|
|||||||
if location_bookmark_name not in bookmark_in_table_position:
|
if location_bookmark_name not in bookmark_in_table_position:
|
||||||
raise ValueError(f"未找到书签 {location_bookmark_name} 对应的表格")
|
raise ValueError(f"未找到书签 {location_bookmark_name} 对应的表格")
|
||||||
|
|
||||||
handle_table_index = bookmark_in_table_position[location_bookmark_name]["tableIndex"]
|
handle_table_index = bookmark_in_table_position[location_bookmark_name][
|
||||||
|
"tableIndex"
|
||||||
|
]
|
||||||
|
|
||||||
handle_table = tables.getByIndex(handle_table_index)
|
handle_table = tables.getByIndex(handle_table_index)
|
||||||
row_count = handle_table.getRows().getCount()
|
row_count = handle_table.getRows().getCount()
|
||||||
@ -419,15 +358,7 @@ def DeleteRow(location_bookmark_name, start_row_index, delete_row_count=-1):
|
|||||||
try:
|
try:
|
||||||
handle_table.getRows().removeByIndex(start_row_index, delete_row_count)
|
handle_table.getRows().removeByIndex(start_row_index, delete_row_count)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise RuntimeError(f"删除行时发生错误: {str(e)}")
|
raise RuntimeError(f"err: {str(e)}")
|
||||||
|
|
||||||
|
|
||||||
def DebugCallDeleteRow():
|
|
||||||
"""
|
|
||||||
调试调用
|
|
||||||
"""
|
|
||||||
DeleteRow("h1", 1, 2)
|
|
||||||
|
|
||||||
|
|
||||||
def LocateBookmark(bookmark_name):
|
def LocateBookmark(bookmark_name):
|
||||||
"""
|
"""
|
||||||
@ -438,7 +369,7 @@ def LocateBookmark(bookmark_name):
|
|||||||
doc = XSCRIPTCONTEXT.getDocument()
|
doc = XSCRIPTCONTEXT.getDocument()
|
||||||
|
|
||||||
if False == doc.getBookmarks().hasByName(bookmark_name):
|
if False == doc.getBookmarks().hasByName(bookmark_name):
|
||||||
return
|
return
|
||||||
|
|
||||||
bookmark = doc.getBookmarks().getByName(bookmark_name)
|
bookmark = doc.getBookmarks().getByName(bookmark_name)
|
||||||
|
|
||||||
@ -467,11 +398,6 @@ def LocateBookmark(bookmark_name):
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise RuntimeError(f"定位书签失败: {str(e)}") from e
|
raise RuntimeError(f"定位书签失败: {str(e)}") from e
|
||||||
|
|
||||||
def DebugCallLocateBookmarkLo1():
|
|
||||||
LocateBookmark("lo1")
|
|
||||||
|
|
||||||
def DebugCallLocateBookmarkLo2():
|
|
||||||
LocateBookmark("lo2")
|
|
||||||
|
|
||||||
def InsertBookmark(bookmark_name):
|
def InsertBookmark(bookmark_name):
|
||||||
"""
|
"""
|
||||||
@ -481,21 +407,21 @@ def InsertBookmark(bookmark_name):
|
|||||||
"""
|
"""
|
||||||
doc = XSCRIPTCONTEXT.getDocument()
|
doc = XSCRIPTCONTEXT.getDocument()
|
||||||
controller = doc.getCurrentController()
|
controller = doc.getCurrentController()
|
||||||
|
|
||||||
# 获取当前选区
|
# 获取当前选区
|
||||||
selection = controller.getSelection()
|
selection = controller.getSelection()
|
||||||
|
|
||||||
# 检查是否已存在同名书签,如果存在则先删除
|
# 检查是否已存在同名书签,如果存在则先删除
|
||||||
bookmarks = doc.getBookmarks()
|
bookmarks = doc.getBookmarks()
|
||||||
if bookmarks.hasByName(bookmark_name):
|
if bookmarks.hasByName(bookmark_name):
|
||||||
bookmarks.getByName(bookmark_name).dispose()
|
bookmarks.getByName(bookmark_name).dispose()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# 创建书签
|
# 创建书签
|
||||||
if selection.getCount() > 0:
|
if selection.getCount() > 0:
|
||||||
# 尝试获取选区的第一个元素
|
# 尝试获取选区的第一个元素
|
||||||
range_to_bookmark = selection.getByIndex(0)
|
range_to_bookmark = selection.getByIndex(0)
|
||||||
|
|
||||||
# 创建书签并附加到选区
|
# 创建书签并附加到选区
|
||||||
bookmark = doc.createInstance("com.sun.star.text.Bookmark")
|
bookmark = doc.createInstance("com.sun.star.text.Bookmark")
|
||||||
bookmark.attach(range_to_bookmark)
|
bookmark.attach(range_to_bookmark)
|
||||||
@ -504,21 +430,13 @@ def InsertBookmark(bookmark_name):
|
|||||||
# 没有选中内容,创建点书签
|
# 没有选中内容,创建点书签
|
||||||
cursor = controller.getViewCursor()
|
cursor = controller.getViewCursor()
|
||||||
text_cursor = cursor.getText().createTextCursorByRange(cursor)
|
text_cursor = cursor.getText().createTextCursorByRange(cursor)
|
||||||
|
|
||||||
# 创建书签并附加到光标位置
|
# 创建书签并附加到光标位置
|
||||||
bookmark = doc.createInstance("com.sun.star.text.Bookmark")
|
bookmark = doc.createInstance("com.sun.star.text.Bookmark")
|
||||||
bookmark.attach(text_cursor)
|
bookmark.attach(text_cursor)
|
||||||
bookmark.setName(bookmark_name)
|
bookmark.setName(bookmark_name)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise RuntimeError(f"插入书签失败: {str(e)}") from e
|
raise RuntimeError(f"插入书签失败: {str(e)}") from e
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def DebugCallInsertBookmark():
|
|
||||||
"""
|
|
||||||
调试调用
|
|
||||||
"""
|
|
||||||
InsertBookmark("bookmark_" + str(time.time()))
|
|
||||||
|
|
||||||
|
|
||||||
def UpdateBookmark(bookmark_name, new_value):
|
def UpdateBookmark(bookmark_name, new_value):
|
||||||
@ -541,11 +459,6 @@ def UpdateBookmark(bookmark_name, new_value):
|
|||||||
old_bookmark = bookmarks.getByName(bookmark_name)
|
old_bookmark = bookmarks.getByName(bookmark_name)
|
||||||
old_bookmark.setName(new_value)
|
old_bookmark.setName(new_value)
|
||||||
|
|
||||||
def DebugCallUpdateBookmark():
|
|
||||||
"""
|
|
||||||
调试调用
|
|
||||||
"""
|
|
||||||
UpdateBookmark("ole_bookmark", "new_bookmark")
|
|
||||||
|
|
||||||
def DeleteBookmark(bookmark_name):
|
def DeleteBookmark(bookmark_name):
|
||||||
"""
|
"""
|
||||||
@ -566,13 +479,6 @@ def DeleteBookmark(bookmark_name):
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def DebugCallDeleteBookmark():
|
|
||||||
"""
|
|
||||||
调试调用
|
|
||||||
"""
|
|
||||||
DeleteBookmark("remove_me")
|
|
||||||
|
|
||||||
|
|
||||||
def CreateTable(location_bookmark_name, data):
|
def CreateTable(location_bookmark_name, data):
|
||||||
"""
|
"""
|
||||||
创建表格
|
创建表格
|
||||||
@ -581,6 +487,7 @@ def CreateTable(location_bookmark_name, data):
|
|||||||
data: 二维数组,用于填充表格数据
|
data: 二维数组,用于填充表格数据
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
def ReplaceTextAndInsertTableRow(json_str):
|
def ReplaceTextAndInsertTableRow(json_str):
|
||||||
"""
|
"""
|
||||||
替换文本和表格
|
替换文本和表格
|
||||||
@ -589,23 +496,27 @@ def ReplaceTextAndInsertTableRow(json_str):
|
|||||||
Replace(data["text"])
|
Replace(data["text"])
|
||||||
BatchInsertRow(data["table"])
|
BatchInsertRow(data["table"])
|
||||||
|
|
||||||
|
|
||||||
def ReplaceTextAndInsertTableRowWithContentControl(json_str):
|
def ReplaceTextAndInsertTableRowWithContentControl(json_str):
|
||||||
data = json.loads(json_str)
|
data = json.loads(json_str)
|
||||||
ReplaceBookmarksWithControls(data["text"])
|
ReplaceBookmarksWithControls(data["text"])
|
||||||
BatchInsertRowWithContentControl(data["table"])
|
BatchInsertRowWithContentControl(data["table"])
|
||||||
|
|
||||||
|
|
||||||
def returnWithJSON(data):
|
def returnWithJSON(data):
|
||||||
return json.dumps(data, ensure_ascii=False)
|
return json.dumps(data, ensure_ascii=False)
|
||||||
|
|
||||||
|
|
||||||
def SaveDocument():
|
def SaveDocument():
|
||||||
# 获取当前文档对象
|
# 获取当前文档对象
|
||||||
model = XSCRIPTCONTEXT.getDocument()
|
model = XSCRIPTCONTEXT.getDocument()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# 检查文档是否支持保存(XStorable 接口)
|
# 检查文档是否支持保存(XStorable 接口)
|
||||||
from com.sun.star.frame import XStorable
|
from com.sun.star.frame import XStorable
|
||||||
|
|
||||||
xstorable = model.uno_getAdapter(XStorable)
|
xstorable = model.uno_getAdapter(XStorable)
|
||||||
|
|
||||||
# 调用保存方法(覆盖原文件)
|
# 调用保存方法(覆盖原文件)
|
||||||
xstorable.store()
|
xstorable.store()
|
||||||
return True
|
return True
|
||||||
@ -613,6 +524,7 @@ def SaveDocument():
|
|||||||
print("err:", e)
|
print("err:", e)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
def DebugCallReplaceBookmarksWithControls():
|
def DebugCallReplaceBookmarksWithControls():
|
||||||
ReplaceBookmarksWithControls({"合同编号_A": "11111", "合同名称_A": "22222"})
|
ReplaceBookmarksWithControls({"合同编号_A": "11111", "合同名称_A": "22222"})
|
||||||
|
|
||||||
@ -621,39 +533,45 @@ def ReplaceBookmarksWithControls(bookmark_name_value_map={}):
|
|||||||
doc = XSCRIPTCONTEXT.getDocument()
|
doc = XSCRIPTCONTEXT.getDocument()
|
||||||
bookmarks = doc.getBookmarks()
|
bookmarks = doc.getBookmarks()
|
||||||
|
|
||||||
for name in reversed([bm.getName() for bm in bookmarks]):
|
for i in reversed(range(bookmarks.getCount())):
|
||||||
try:
|
bookmark = bookmarks.getByIndex(i)
|
||||||
if name.startswith('_'):
|
name = bookmark.getName()
|
||||||
continue
|
anchor = bookmark.getAnchor()
|
||||||
bookmark = bookmarks.getByName(name)
|
if not anchor:
|
||||||
anchor = bookmark.getAnchor()
|
continue
|
||||||
|
|
||||||
|
original_text = anchor.getString()
|
||||||
|
replace_value = bookmark_name_value_map.get(name, original_text)
|
||||||
|
|
||||||
|
# === 第1步:创建内容控件 ===
|
||||||
|
content_control = doc.createInstance("com.sun.star.text.ContentControl")
|
||||||
|
|
||||||
|
# 获取书签所在文本对象
|
||||||
|
text = anchor.getText()
|
||||||
|
anchor.setString("")
|
||||||
|
# # 创建游标并清空原内容
|
||||||
|
|
||||||
|
# 插入内容控件
|
||||||
|
text.insertTextContent(anchor, content_control, True)
|
||||||
|
|
||||||
|
# 设置控件内容
|
||||||
|
cc_cursor = content_control.getText().createTextCursor()
|
||||||
|
cc_cursor.setString(replace_value)
|
||||||
|
|
||||||
# 获取书签内容
|
# === 第1步:清理旧书签 ===
|
||||||
cursor = anchor.getText().createTextCursorByRange(anchor)
|
bookmark.dispose()
|
||||||
original_text = cursor.getString()
|
|
||||||
|
|
||||||
# 创建文本控件并设置内容
|
# === 第2步:创建新书签 ===
|
||||||
text_control = doc.createInstance("com.sun.star.text.TextField.Input")
|
# 获取控件实际范围
|
||||||
if (bookmark_name_value_map.get(name)):
|
cc_anchor = content_control.getText().getAnchor()
|
||||||
text_control.setPropertyValue("Content", bookmark_name_value_map.get(name))
|
|
||||||
else
|
|
||||||
text_control.setPropertyValue("Content", original_text)
|
|
||||||
|
|
||||||
# 替换原书签内容为控件
|
new_bookmark = doc.createInstance("com.sun.star.text.Bookmark")
|
||||||
cursor.setString("")
|
new_bookmark.setName(name)
|
||||||
anchor.getText().insertTextContent(cursor, text_control, False)
|
new_bookmark.attach(cc_anchor)
|
||||||
|
|
||||||
# 重新插入原书签到控件后
|
|
||||||
new_cursor = cursor.getText().createTextCursorByRange(cursor)
|
|
||||||
new_cursor.goRight(1, False)
|
|
||||||
doc.getBookmarks().addNew(name, new_cursor)
|
|
||||||
|
|
||||||
except Exception as e:
|
|
||||||
print(f"error{str(e)}")
|
|
||||||
|
|
||||||
def NextEditableZone(current_index):
|
def NextEditableZone(current_index):
|
||||||
"""
|
"""
|
||||||
根据索引定位可编辑区域(支持循环)
|
根据索引定位可编辑的内容控件(支持循环)
|
||||||
Args:
|
Args:
|
||||||
current_index (int/str): 从 0 开始的索引,支持字符串或整数类型
|
current_index (int/str): 从 0 开始的索引,支持字符串或整数类型
|
||||||
"""
|
"""
|
||||||
@ -661,40 +579,39 @@ def NextEditableZone(current_index):
|
|||||||
try:
|
try:
|
||||||
current_index = int(current_index)
|
current_index = int(current_index)
|
||||||
except (ValueError, TypeError):
|
except (ValueError, TypeError):
|
||||||
print(f"err {current_index} cant convert to int")
|
print(f"错误:无法将 {current_index} 转换为整数")
|
||||||
return
|
return
|
||||||
|
|
||||||
doc = XSCRIPTCONTEXT.getDocument()
|
doc = XSCRIPTCONTEXT.getDocument()
|
||||||
controller = doc.getCurrentController()
|
controller = doc.getCurrentController()
|
||||||
view_cursor = controller.getViewCursor()
|
view_cursor = controller.getViewCursor()
|
||||||
|
|
||||||
# 收集所有可编辑控件的锚点
|
# 收集所有内容控件的锚点
|
||||||
text_fields = doc.getTextFields().createEnumeration()
|
text_content = doc.getText().createEnumeration()
|
||||||
editable_anchors = []
|
editable_anchors = []
|
||||||
while text_fields.hasMoreElements():
|
while text_content.hasMoreElements():
|
||||||
field = text_fields.nextElement()
|
element = text_content.nextElement()
|
||||||
if field.supportsService("com.sun.star.text.TextField.Input"):
|
# 关键修改点:检查内容控件服务
|
||||||
|
if element.supportsService("com.sun.star.text.ContentControl"):
|
||||||
try:
|
try:
|
||||||
anchor = field.getAnchor()
|
anchor = element.getAnchor()
|
||||||
editable_anchors.append(anchor)
|
editable_anchors.append(anchor)
|
||||||
except Exception:
|
except Exception:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# 无控件时直接返回
|
# 无控件时直接返回
|
||||||
if not editable_anchors:
|
if not editable_anchors:
|
||||||
print("文档中无可编辑控件")
|
print("文档中未找到内容控件")
|
||||||
return
|
return
|
||||||
|
|
||||||
# 计算有效索引(循环逻辑)
|
# 计算有效索引(循环逻辑)
|
||||||
total = len(editable_anchors)
|
total = len(editable_anchors)
|
||||||
effective_index = current_index % total # 此处不再报错
|
effective_index = current_index % total # 自动处理越界
|
||||||
|
|
||||||
# 定位到目标控件
|
# 定位到目标控件
|
||||||
target_anchor = editable_anchors[effective_index]
|
target_anchor = editable_anchors[effective_index]
|
||||||
view_cursor.gotoRange(target_anchor, False)
|
view_cursor.gotoRange(target_anchor.getStart(), False) # 跳转到控件起始位置
|
||||||
controller.select(target_anchor) # 选中控件(可选)
|
controller.select(target_anchor) # 选中整个控件范围
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -710,16 +627,9 @@ g_exportedScripts = (
|
|||||||
ReplaceTextAndInsertTableRow,
|
ReplaceTextAndInsertTableRow,
|
||||||
DebugCallReplace,
|
DebugCallReplace,
|
||||||
DebugCallReplaceList,
|
DebugCallReplaceList,
|
||||||
DebugCallInsertRow,
|
|
||||||
DebugCallDeleteRow,
|
|
||||||
DebugCallLocateBookmarkLo1,
|
|
||||||
DebugCallLocateBookmarkLo2,
|
|
||||||
DebugCallInsertBookmark,
|
|
||||||
DebugCallUpdateBookmark,
|
|
||||||
DebugCallDeleteBookmark,
|
|
||||||
ReplaceBookmarksWithControls,
|
ReplaceBookmarksWithControls,
|
||||||
ReplaceTextAndInsertTableRowWithContentControl,
|
ReplaceTextAndInsertTableRowWithContentControl,
|
||||||
DebugCallReplaceBookmarksWithControls,
|
DebugCallReplaceBookmarksWithControls,
|
||||||
SaveDocument,
|
SaveDocument,
|
||||||
NextEditableZone
|
NextEditableZone,
|
||||||
)
|
)
|
||||||
|
Loading…
Reference in New Issue
Block a user