如何使用Python导入CSV删除或更新Google电子表格?

发布于 2025-02-01 02:53:55 字数 640 浏览 3 评论 0原文

我正在处理我的Python脚本,现在我的脚本可以将CSV导入电子表格,但是我必须对其进行修改以替换或删除已经导入的旧电子表格并将其更新为最新的CSV,因为当前我的脚本仅在上传和在CSV文件上的数据粘贴。我需要修改脚本什么?

我的脚本


def export_csv(o_csv, sheet_id):
    with open(o_csv, 'r') as csv_file:

       csvContents = csv_file.read()
    body = {
        'requests': [{
            'pasteData': {
                "coordinate": {
                    "sheetId": sheet_id,
                    "rowIndex": "0",
                    "columnIndex": "0",
                },
                "data": csvContents,
                "type": 'PASTE_NORMAL',
                "delimiter": ',',
            }
        }]
    }

I'm working on my Python script and right now my script can import csv to spreadsheet, but I have to modify it to replace or delete the old spreadsheet that already imported and update it to the latest csv, since currently my script only working in upload and normal paste of data on csv file. What do I need to modify my script?

My script


def export_csv(o_csv, sheet_id):
    with open(o_csv, 'r') as csv_file:

       csvContents = csv_file.read()
    body = {
        'requests': [{
            'pasteData': {
                "coordinate": {
                    "sheetId": sheet_id,
                    "rowIndex": "0",
                    "columnIndex": "0",
                },
                "data": csvContents,
                "type": 'PASTE_NORMAL',
                "delimiter": ',',
            }
        }]
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

秋心╮凉 2025-02-08 02:53:55

我相信您的目标如下。

  • 清除Sheet_id的表之后,您要将CSV数据放在Sheet_id的表格上。
  • 您想使用python的gspread实现这一目标。

在这种情况下,以下修改如何?

来自:

body = {
    'requests': [{
        'pasteData': {
            "coordinate": {
                "sheetId": sheet_id,
                "rowIndex": "0",
                "columnIndex": "0",
            },
            "data": csvContents,
            "type": 'PASTE_NORMAL',
            "delimiter": ',',
        }
    }]
}

到:

body = {
    "requests": [
        {"updateCells": {"range": {"sheetId": sheet_id}, "fields": "*"}}, # Added
        {
            "pasteData": {
                "coordinate": {
                    "sheetId": sheet_id,
                    "rowIndex": "0",
                    "columnIndex": "0",
                },
                "data": csvContents,
                "type": "PASTE_NORMAL",
                "delimiter": ",",
            }
        },
    ]
}
  • 在这种情况下,通过将一个请求添加到您的请求正文中,清除了Sheet_id的表格。并且,清除工作表后,将放置CSV数据。这样,可以通过一个API调用来完成此请求。
  • 在这种情况下,字段使用*。如果要控制清晰的内容,请修改此内容。

参考:

I believe your goal is as follows.

  • After the sheet of sheet_id is cleared, you want to put the CSV data to the sheet of sheet_id.
  • You want to achieve this using gspread for python.

In this case, how about the following modification?

From:

body = {
    'requests': [{
        'pasteData': {
            "coordinate": {
                "sheetId": sheet_id,
                "rowIndex": "0",
                "columnIndex": "0",
            },
            "data": csvContents,
            "type": 'PASTE_NORMAL',
            "delimiter": ',',
        }
    }]
}

To:

body = {
    "requests": [
        {"updateCells": {"range": {"sheetId": sheet_id}, "fields": "*"}}, # Added
        {
            "pasteData": {
                "coordinate": {
                    "sheetId": sheet_id,
                    "rowIndex": "0",
                    "columnIndex": "0",
                },
                "data": csvContents,
                "type": "PASTE_NORMAL",
                "delimiter": ",",
            }
        },
    ]
}
  • In this case, by adding one request to your request body, the sheet of sheet_id is cleared. And, after the sheet was cleared, the CSV data is put. By this, this request can be done by one API call.
  • In this case, fields use *. If you want to control the clear contents, please modify this.

References:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文