【问题标题】:Save google spreadsheet as new将谷歌电子表格另存为新的
【发布时间】:2017-11-06 15:44:26
【问题描述】:

更新显示在我的问题末尾。

我能够使用 Python 和 gspread 创建一个新的谷歌电子表格:

import gspread
from oauth2client.service_account import ServiceAccountCredentials

from pprint import pprint
from googleapiclient import discovery

scope = ['https://spreadsheets.google.com/feeds',
    'https://www.googleapis.com/auth/drive']
credentials = ServiceAccountCredentials.from_json_keyfile_name('credentials.json', scope)
gc = gspread.authorize(credentials)

service = discovery.build('sheets', 'v4', credentials=credentials)

spreadsheet_body = {
"properties": {
    "title": "xxGoogleAPIMasterTemplatexx"
  }
}

request = service.spreadsheets().create(body=spreadsheet_body)
response = request.execute()

gc.insert_permission(response['spreadsheetId'], 'xxxxx@zzzz-yyyyyy.iam.gserviceaccount.com', perm_type='user', role='owner')
gc.insert_permission(response['spreadsheetId'], 'xxxx.xxxx@gmail.com', perm_type='user', role='owner')

效果很好。

我的目标是使用模板谷歌电子表格,复制它然后编辑副本(一些单元格 --> 这很简单,我可以确认我已经完成了这部分)并重命名它.

我想做的是:

  • 复制现有电子表格。然后我只需要知道复制的电子表格的名称或 ID。
  • 打开现有的电子表格并另存为新的。
  • 在上面的脚本中添加电子表格正文。更具体地说,编辑这部分:

    电子表格正文 = { “特性”: { "title": "xxGoogleAPIMasterTemplatexx" } }

为了使它包含单元格中的所有标题和值。那可能吗?如何查看现有电子表格中的属性?

我还没有找到有用的东西。我只找到了如何将工作表(标签)从一个谷歌电子表格复制到另一个。

PS:我正在使用 API V4。

更新:

我遵循了我在这个问题中得到的第一个答案。因此,我创建了一个脚本,可以将工作表复制到另一个电子表格中。

import gspread
from oauth2client.service_account import ServiceAccountCredentials

from pprint import pprint
from googleapiclient import discovery

scope = ['https://spreadsheets.google.com/feeds',
    'https://www.googleapis.com/auth/drive']
credentials = ServiceAccountCredentials.from_json_keyfile_name('credentials.json', scope)
gc = gspread.authorize(credentials)

service = discovery.build('sheets', 'v4', credentials=credentials)

# The ID of the spreadsheet containing the sheet to copy.
spreadsheet_id = 'xxxxxx'   

# The ID of the sheet to copy.
sheet_id = 730266781  # TODO: Update placeholder value.

copy_sheet_to_another_spreadsheet_request_body = {
    # The ID of the spreadsheet to copy the sheet to.
    'destination_spreadsheet_id': 'yyyy',  

    # TODO: Add desired entries to the request body.
}

request = service.spreadsheets().sheets().copyTo(spreadsheetId=spreadsheet_id, sheetId=sheet_id, body=copy_sheet_to_another_spreadsheet_request_body)
response = request.execute()

# TODO: Change code below to process the `response` dict:
pprint(response)

但是我收到了这个错误:

https://sheets.googleapis.com/v4/spreadsheets/xxxxxx/sheets/730266781:copyTo?alt=json 返回“调用者没有权限”>

所以,我首先尝试向我的 credentials.json 文件 (xxxx-yyyy@bbbbb-182311.iam.gserviceaccount.com) 中提到的用户授予访问权限

然后我也尝试使用这一行:

gc.insert_permission(response['spreadsheetId'], 'xxxx-yyyy@bbbbb-182311.iam.gserviceaccount.com', perm_type='user', role='owner')

我无法找到解决方案,因此我跳过了这一部分。我公开了模板。

【问题讨论】:

    标签: python google-apps-script google-api gspread


    【解决方案1】:

    以下解决方法怎么样?我认为有两种模式。

    模式一:

    1. 使用service.spreadsheets().create() 创建带有{ "properties": { "title": "xxGoogleAPIMasterTemplatexx" } } 的新电子表格。
      • 可以返回文件 ID 和文件名。
    2. 使用service.spreadsheets().sheets().copyTo() 将模板电子表格复制到新电子表格。

    在这种情况下,仅使用 Sheets API。

    模式二:

    1. 使用service.files().copy() 复制标题为{"name": "xxGoogleAPIMasterTemplatexx"} 的模板电子表格。
      • 可以返回文件 ID 和文件名。

    在这种情况下,仅使用 Drive API。

    关于其他问题:

    1. 问: 以使其包含单元格中的所有标题和值。那可能吗?
      • 答:这可以通过复制模板表来实现。这对于两种模式都是可能的。如果要将其他电子表格中的数据复制到复制的电子表格中,可以使用 values.get 和 values.update。
    2. 问:如何查看现有电子表格的属性?
      • 答:您可以使用service.spreadsheets().get() 作为fields=properties 检索属性。

    参考资料:

    您可以在这些参考资料中查看示例脚本。

    如果我误解了你的问题,我很抱歉。

    编辑:

    要删除带有id=0 的工作表,您可以使用以下脚本。

    batch_update_spreadsheet_request_body = {
      "requests": [{
        "deleteSheet": {
          "sheetId": 0
        }
      }]
    }
    service.spreadsheets().batchUpdate(spreadsheetId=spreadsheet_id, body=batch_update_spreadsheet_request_body)
    

    参考:

    【讨论】:

    • 我正在努力。到目前为止,我有两个问题。权限错误以及我无法将电子表格 ID 动态放入大括号部分 (copy_sheet_to_another_spreadsheet_request_body) 的事实。我会尽快更新问题。
    • 第一个选项有效。下一步是删除第一个工作表 (id=0)。谢谢:)
    • @Apolo Radomer 欢迎您。也谢谢你。我更新了我的答案。请确认。
    • 这就是我尝试并失败的方法,因为我还在请求中添加了 sheetId。我删除了它,它工作正常。结果与您的代码相同:)
    • @Apolo Radomer 感谢您提供更多信息。
    猜你喜欢
    • 2014-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多