feat: replace bookmark and insert table row
This commit is contained in:
parent
ad1506790b
commit
9d4df1df84
@ -186,10 +186,6 @@ def InsertRow(location_bookmark_name, data, start_row_index=-1):
|
|||||||
data: 二维数组,用于填充表格数据
|
data: 二维数组,用于填充表格数据
|
||||||
start_row_index: 起始行位置,默认为-1,即在表格末尾插入
|
start_row_index: 起始行位置,默认为-1,即在表格末尾插入
|
||||||
"""
|
"""
|
||||||
# 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()
|
||||||
@ -215,19 +211,6 @@ def InsertRow(location_bookmark_name, data, start_row_index=-1):
|
|||||||
raise ValueError(f"数据列数不匹配,表格有 {col_count} 列")
|
raise ValueError(f"数据列数不匹配,表格有 {col_count} 列")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# for _ in range(len(data)):
|
|
||||||
# if start_row_index > 0 & start_row_index < row_count:
|
|
||||||
# handle_table.getRows().insertByIndex(start_row_index, 1)
|
|
||||||
# else:
|
|
||||||
# handle_table.getRows().insertByIndex(row_count, 1)
|
|
||||||
|
|
||||||
# for row_idx, row_data in enumerate(data, start=row_count):
|
|
||||||
# for col_idx, cell_value in enumerate(row_data):
|
|
||||||
# cell = handle_table.getCellByPosition(col_idx, row_idx)
|
|
||||||
# cell.setString(str(cell_value))
|
|
||||||
|
|
||||||
# 获取需要插入的行数
|
|
||||||
# 获取表格当前总行数
|
|
||||||
row_count = handle_table.getRows().getCount()
|
row_count = handle_table.getRows().getCount()
|
||||||
rows_to_insert = len(data)
|
rows_to_insert = len(data)
|
||||||
|
|
||||||
@ -252,6 +235,65 @@ def InsertRow(location_bookmark_name, data, start_row_index=-1):
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise RuntimeError(f"插入行时发生错误: {str(e)}")
|
raise RuntimeError(f"插入行时发生错误: {str(e)}")
|
||||||
|
|
||||||
|
def BatchInsertRow(data_array):
|
||||||
|
"""
|
||||||
|
批量插入行
|
||||||
|
"""
|
||||||
|
|
||||||
|
doc = XSCRIPTCONTEXT.getDocument()
|
||||||
|
tables = doc.getTextTables()
|
||||||
|
bookmarks = doc.getBookmarks()
|
||||||
|
bookmark_in_table_position = QueryBookmarkPositionInTable(tables, bookmarks)
|
||||||
|
|
||||||
|
for arr_obj in data_array:
|
||||||
|
location_bookmark_name = arr_obj.get('location_bookmark_name')
|
||||||
|
data = arr_obj.get('data')
|
||||||
|
start_row_index = arr_obj.get('start_row_index', -1) # 默认值为 -1
|
||||||
|
if location_bookmark_name not in bookmark_in_table_position:
|
||||||
|
raise ValueError(f"未找到书签 {location_bookmark_name} 对应的表格")
|
||||||
|
|
||||||
|
handle_table_index = bookmark_in_table_position[location_bookmark_name]["tableIndex"]
|
||||||
|
|
||||||
|
try:
|
||||||
|
handle_table = tables.getByIndex(handle_table_index)
|
||||||
|
except IndexError:
|
||||||
|
raise IndexError(f"表格索引 {handle_table_index} 超出范围")
|
||||||
|
|
||||||
|
col_count = handle_table.getColumns().getCount()
|
||||||
|
row_count = handle_table.getRows().getCount()
|
||||||
|
|
||||||
|
if not data or len(data) == 0:
|
||||||
|
return
|
||||||
|
|
||||||
|
if any(len(row) != col_count for row in data):
|
||||||
|
raise ValueError(f"数据列数不匹配,表格有 {col_count} 列")
|
||||||
|
|
||||||
|
try:
|
||||||
|
row_count = handle_table.getRows().getCount()
|
||||||
|
rows_to_insert = len(data)
|
||||||
|
|
||||||
|
if rows_to_insert > 0:
|
||||||
|
# 确定插入位置
|
||||||
|
if start_row_index != -1 and 0 <= start_row_index < row_count:
|
||||||
|
insert_pos = start_row_index + 1 # 在指定行下方插入
|
||||||
|
else:
|
||||||
|
insert_pos = row_count # 插入到表格末尾
|
||||||
|
|
||||||
|
# 批量插入所有新行
|
||||||
|
handle_table.getRows().insertByIndex(insert_pos, rows_to_insert)
|
||||||
|
|
||||||
|
# 填充数据到新插入的行
|
||||||
|
for data_row_idx, row_data in enumerate(data):
|
||||||
|
target_row = insert_pos + data_row_idx # 计算目标行索引
|
||||||
|
for col_idx, cell_value in enumerate(row_data):
|
||||||
|
cell = handle_table.getCellByPosition(col_idx, target_row)
|
||||||
|
cell.setString(str(cell_value))
|
||||||
|
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
raise RuntimeError(f"插入行时发生错误: {str(e)}")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def InsertRowWithJSONDefineCol(json_str):
|
def InsertRowWithJSONDefineCol(json_str):
|
||||||
"""
|
"""
|
||||||
@ -471,6 +513,14 @@ def CreateTable(location_bookmark_name, data):
|
|||||||
data: 二维数组,用于填充表格数据
|
data: 二维数组,用于填充表格数据
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
def ReplaceTextAndInsertTableRow(json_str):
|
||||||
|
"""
|
||||||
|
替换文本和表格
|
||||||
|
"""
|
||||||
|
data = json.loads(json_str)
|
||||||
|
Replace(data["text"])
|
||||||
|
BatchInsertRow(data["table"])
|
||||||
|
|
||||||
def returnWithJSON(data):
|
def returnWithJSON(data):
|
||||||
return json.dumps(data, ensure_ascii=False)
|
return json.dumps(data, ensure_ascii=False)
|
||||||
|
|
||||||
@ -483,6 +533,7 @@ g_exportedScripts = (
|
|||||||
InsertRowWithJSON,
|
InsertRowWithJSON,
|
||||||
DeleteRow,
|
DeleteRow,
|
||||||
DeleteRowWithJSON,
|
DeleteRowWithJSON,
|
||||||
|
ReplaceTextAndInsertTableRow,
|
||||||
DebugCallReplace,
|
DebugCallReplace,
|
||||||
DebugCallReplaceList,
|
DebugCallReplaceList,
|
||||||
DebugCallInsertRow,
|
DebugCallInsertRow,
|
||||||
|
Loading…
Reference in New Issue
Block a user