将一行复制到下一行并使用 gspread python 更新新行中的内容

问题描述 投票:0回答:1

我有一个 JSON 文件,我期待在解析该文件内容后将其放入 Google 工作表中。我正在使用 gspread for Python 以编程方式与 Google Sheets 进行交互。工作表中数据的格式包含一些带有复选框的列,可以勾选或不勾选(基于 TRUE 和 FALSE 值)、一些简单的条目(例如姓名和联系电话)以及一些下拉菜单。

json 中的内容直接且易于解析,因此我使用的方法是将第一行保留为工作表中的默认行,并将其用作所有应该包含的数据的模板摄入到床单中。

默认情况下,模板行具有空复选框和空下拉列表,因此在处理我的记录时,我尝试为每个新索引创建此模板行的副本,并根据 json 解析的记录数据更新该索引中的内容。我很难复制与前一行完全相同的内容,因为我希望每个新行条目都存在复选框和下拉列表,并根据输入值更新值。 例如:我的 Google 工作表包含行业名称作为列名称,默认复选框设置为未选中,如果我处理的记录包含对此行业的任何兴趣,那么我需要勾选该复选框值。

这种复制前一行的方法可能不是理想的方法,但我愿意接受任何建议。我有关于列值是 TRUE 或 FALSE 的信息,但是否可以通过编程将它们映射到复选框?

注意:我知道Google App Script可以轻松管理工作表的内容,但我想使用gspread来实现这一点,以供将来参考,我将期待从其他工作表中提取数据并解析这个新的数据片材。

样本记录

records = [{
    "timestamp": "2023-08-14 12:45:00.127784",
    "legacyID": "CRM01",
    "firstName": "David",
    "lastName": "Richards",
    "preferredName": "",
    "preferredPronouns": "",
    "interests": [
        "Semiconductors"
    ],
    "eligibility": [],
    "lead": {
        "status": "Subscribed",
        "source": "",
        "sourcePath": "",
        "value": "",
        "reliability": "",
        "onBoardDate": "",
        "lastContactDate": "",
        "offBoardDate": ""
    },
    "segmentation": {
        "organisation": "M UK Limited"
        
    },
    "audit": {
        "createdDate": "2023-08-14 12:45:00.128195",
        "createdBy": "Admin",
        "lastModifiedDate": "",
        "lastModifiedBy": "",
        "deletedDate": None,
        "deletedBy": None,
        "deleteRequestedBy": None,
        "auditor_email": ""
    },
    "comments": []
}]

start_index = 7

template_row = worksheet2.row_values(start_index)

for idx, record in enumerate(records, start_index + 1):
    new_row_index = start_index + 1
    worksheet2.insert_row(template_row, index=new_row_index)
    updated_row = template_row.copy()
    checkbox_columns = [
    "FinTech", "MedTech", "Advanced Manufacturing", "Creative", "Net Zero",
    "Semiconductors", "Digital", "Business", "Software Development",
    "Data Science", "Cyber Security", "Games Development", "Repair",
    "Business Development", "Entrepreneurship/Intrapreneurship",
    "Mental Health", "Skills Academies", "Professional & Upskilling",
    "Apprenticeships"
]

  interests_arr = record['interests']

  for column_name in checkbox_columns:
      if column_name in interests_arr:
         updated_row[worksheet2.find(column_name).col - 1] = "TRUE"
      else:
         updated_row[worksheet2.find(column_name).col - 1] = "FALSE"
   
   worksheet2.update(updated_row, index=idx)

在示例记录中,兴趣列存储其中一个行业名称的 ID,我通过从数据库查询来获取行业名称,并且我希望选中相关行业名称的复选框。

上面是数据进入的电子表格的屏幕截图。我无法将这些 TRUE 和 FALSE 值以编程方式转换为复选框。第一行是我要使用的默认模板行。

python-3.x excel google-sheets gspread
1个回答
0
投票

我相信您的目标如下。

  • 当您的
    interests
    值中的
    records
    值包含在
    checkbox_columns
    的标题中时,您希望将 TRUE 或 FALSE 设置为复选框。
  • 您想使用 gspread 来实现这一点。

当我看到你的显示脚本时,

update
find
在循环中使用。在这种情况下,加工成本变高。此外,当在循环中使用 Sheets API 时,可能会出现与连续请求相关的错误。我很担心这个。

在这种情况下,下面的示例脚本怎么样?在此示例脚本中,通过使用batchUpdate方法,Sheets API仅使用一次来写入值。

示例脚本:

import gspread

client = ### Please use your client for gspread

spreadsheet_key = "###" # Please set Spreadsheet ID.
spreadsheet = client.open_by_key(spreadsheet_key)
worksheet2 = spreadsheet.worksheet("Sheet1") # Please set sheet name.

# This is a simple sample value including "interests" property for testing this script.
records = [
    {"interests": ["Semiconductors", "Repair"]},
    {"interests": []},
    {"interests": ["MedTech", "Digital"]},
    {"interests": ["Entrepreneurship/Intrapreneurship", "Data Science", "Apprenticeships"]},
]

# This is from your showing script.
checkbox_columns = [
    "FinTech", "MedTech", "Advanced Manufacturing", "Creative", "Net Zero",
    "Semiconductors", "Digital", "Business", "Software Development",
    "Data Science", "Cyber Security", "Games Development", "Repair",
    "Business Development", "Entrepreneurship/Intrapreneurship",
    "Mental Health", "Skills Academies", "Professional & Upskilling",
    "Apprenticeships"
]
start_index = 7  # This is from your showing script.

header_len = len(checkbox_columns)
dst_values = []
for e in records:
    if "interests" in e:
        interests = e.get("interests", [])
        if interests == []:
            dst_values.append(
                {"values": [{
                    "dataValidation": {"condition": {"type": "BOOLEAN"}},
                    "userEnteredValue": {"boolValue": False}
                } for _ in range(header_len)]}
            )
        else:
            values = []
            for h in checkbox_columns:
                values.append({
                    "dataValidation": {"condition": {"type": "BOOLEAN"}},
                    "userEnteredValue": {"boolValue": True if h in interests else False}
                })
            dst_values.append({"values": values})

if dst_values != []:
    requests = [{
        "updateCells": {
            "range": {"sheetId": worksheet2.id, "startRowIndex": start_index, "startColumnIndex": 0},
            "rows": dst_values,
            "fields": "dataValidation,userEnteredValue"
        }
    }]

# Request Sheets API.
spreadsheet.batch_update({"requests": requests})
  • 请根据您的实际情况修改此脚本。

  • 在此示例脚本中,复选框是使用 Sheets API 直接创建的。这样就不需要使用模板行了。

测试:

运行该脚本,得到以下结果。

参考:

© www.soinside.com 2019 - 2024. All rights reserved.