Skip to content

Commit

Permalink
V0.9.50 (waditu#196)
Browse files Browse the repository at this point in the history
* V0.9.50
添加飞书表格批量样式更新及条件格式功能

* V0.9.50
添加用于更新电子表格方法
  • Loading branch information
hanzch authored May 12, 2024
1 parent 62521ed commit d1e030c
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions czsc/fsa/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
create_dt: 2022/12/16 19:37
describe:
"""

import requests
import pandas as pd
from loguru import logger
from czsc.fsa.base import request, FeishuApiBase
from czsc.fsa.spreed_sheets import SpreadSheets, SingleSheet
Expand Down Expand Up @@ -137,3 +139,45 @@ def push_message(msg: str, msg_type: str = "text", receive_id_type: str = "open_
logger.error(f"不支持的消息类型:{msg_type}")
except Exception as e:
logger.error(f"推送消息失败:{e}")


def update_spreadsheet(df: pd.DataFrame, spreadsheet_token: str, sheet_id: str, **kwargs) -> int:
"""使用飞书机器人更新电子表格
获取 spreadsheet_token - https://open.feishu.cn/document/server-docs/docs/faq 第7节
获取 sheet_id - https://open.feishu.cn/document/server-docs/docs/sheets-v3/spreadsheet-sheet/query?appId=cli_a3077015cc39500e
:param df: datafream内容
:param spreadsheet_token: 表格对应的token,url获取
:param sheet_id: 工作表的id
:param kwargs:
feishu_app_id: 飞书APP的app_id
feishu_app_secret: 飞书APP的app_secret
:return:
"""
fsf = SpreadSheets(app_id=kwargs['feishu_app_id'], app_secret=kwargs['feishu_app_secret'])

data = {
"valueRanges": [
{
"range": f"{sheet_id}!A1:Z1",
"values": [list(df)]
}, {
"range": f"{sheet_id}!A2:Z5000",
"values": df.values.tolist()
}
]
}
try:
fsf.delete_values(spreadsheet_token, sheet_id)
b = fsf.update_values(spreadsheet_token, data)
if b and b['code'] == 0:
logger.success("更新飞书表格成功")
return 1
else:
logger.error(b)
return 0
except Exception:
logger.exception("更新飞书表格失败")
return 0

0 comments on commit d1e030c

Please sign in to comment.