主题
insert_dbt() 函数
使用insert_dbt()
函数向数据表中写入新记录。
函数签名
python
def insert_dbt(data: dict[str, any] | list[dict[str, any]] | __pd.DataFrame,
sheet_name: str = '',
new_sheet: bool = False) -> None:
参数列表
参数 | 类型 | 默认值 | 说明 |
---|---|---|---|
data | object | 必填 | 要回写到数据表里的数据。 支持的数据类型包括: dict[str, any] 包含记录名称和值的 dict ;维度不超过2维的 list 类型;pandas.DataFrame ;不支持写入附件等复杂类型。 |
sheet_name | str | 空字符串 | 写入数据的选区所在的数据表名称。 当 new_sheet=False 时为表格中已经存在的数据表名称。当 new_sheet=True 时为新建的数据表的名称。 |
new_sheet | bool | False | 是否将数据写入到新建的数据表中。 |
提示
建议直接使用 dbt()
函数返回的 DataFrame
,进行所需要的处理后,回写数据表。
示例
python
# 返回数据表1中的字段名为产品名称和库存数量的记录,表头为字段名
# 返回的数据类似下表所示
# 其中 B,C,D 为 Index 中保存的记录 ID
# +-----+-------------+----------+
# | | 产品名称 | 库存数量 |
# +-----+-------------+----------+
# | B | iPhone 12 | 100 |
# +-----+-------------+----------+
# | C | MacBook Pro | 175 |
# +-----+-------------+----------+
# | D | iPad Air 4 | 173 |
# +-----+-------------+----------+
df = dbt(field=['产品名称', '库存数量'])
# 将 df 中的数据写入到新的数据表2中
insert_dbt(df, sheet_name="数据表2", new_sheet=True)
# 向数据表2中,新增一条记录
insert_dbt({"产品名称": "iPad Air 5", "库存数量": 100 }, sheet_name="数据表2")
# 向数据表2中,新增两条记录
insert_dbt([{"产品名称": "Mac mini M2"}, {"产品名称": "Mac mini M2 Pro"}] , sheet_name="数据表2")