Gspread如何复制sheet

2024-05-21

在 Stackoverflow 上进行谷歌搜索和搜索后,我想我找不到有关如何复制现有工作表(现有模板工作表)并将其保存到另一个工作表中的指南。

根据文档,有重复表 https://gspread.readthedocs.io/en/latest/api.html#gspread.models.Spreadsheet.add_worksheet但我无法做一个有效的例子,有人可以指导我吗?

 import gspread
 from gspread.models import Cell, Spreadsheet

 scope = [
 "https://www.googleapis.com/auth/spreadsheets.readonly",
 "https://www.googleapis.com/auth/spreadsheets",
 "https://www.googleapis.com/auth/drive.readonly",
 "https://www.googleapis.com/auth/drive.file",
 "https://www.googleapis.com/auth/drive",
 ]

 json_key_absolute_path = "key.json"
 credentials = ServiceAccountCredentials.from_json_keyfile_name(json_key_absolute_path, scope)
 client = gspread.authorize(credentials)

 spreadsheet_client = Spreadsheet(client)
 spreadsheet_client.duplicate_sheet("18Qk5bzuA7JOBD8CTgwvKYRiMl_35it5AwcFG2Bi5npo", new_sheet_name="timcard2")
 worksheet = client.open("timcard2")
 worksheet.share("[email protected] /cdn-cgi/l/email-protection", perm_type='user', role='writer')

  • 您想要将源电子表格复制为新电子表格。
  • 您希望使用 gspread 和 python 来实现此目的。
  • 您已经能够使用 Sheets API 获取和输入 Google 电子表格的值。

如果我的理解是正确的,这个答案怎么样?

问题及解决方案:

看起来duplicate_sheetgspread 方法用于将源电子表格中的工作表复制到同一源电子表格中。Ref https://github.com/burnash/gspread/blob/a52db7006f7906eca42fff058deb254526c4cb08/gspread/models.py#L364为了将源电子表格复制为新电子表格,请使用以下方法copy() https://gspread.readthedocs.io/en/latest/api.html#gspread.Client.copy类客户。

示例脚本:

请按如下方式修改您的脚本。

From:
client = gspread.authorize(credentials)

spreadsheet_client = Spreadsheet(client)
spreadsheet_client.duplicate_sheet("18Qk5bzuA7JOBD8CTgwvKYRiMl_35it5AwcFG2Bi5npo", new_sheet_name="timcard2")
worksheet = client.open("timcard2")
worksheet.share("[email protected] /cdn-cgi/l/email-protection", perm_type='user', role='writer')
To:
client = gspread.authorize(credentials)
client.copy("18Qk5bzuA7JOBD8CTgwvKYRiMl_35it5AwcFG2Bi5npo", title="timcard2", copy_permissions=True)

worksheet = client.open("timcard2")
worksheet.share("[email protected] /cdn-cgi/l/email-protection", perm_type='user', role='writer')
  • 当您运行脚本时,电子表格 ID 为18Qk5bzuA7JOBD8CTgwvKYRiMl_35it5AwcFG2Bi5npo被复制为电子表格名称timcard2。并且,源电子表格的权限信息也被复制。

Note:

  • 在这种情况下,当copy_permissions=True使用时,权限信息也会被复制。所以虽然我不确定你的实际情况,但可能不需要使用worksheet.share("[email protected] /cdn-cgi/l/email-protection", perm_type='user', role='writer')。请小心这一点。

参考:

  • 重复表 https://gspread.readthedocs.io/en/latest/api.html#gspread.models.Spreadsheet.duplicate_sheet
  • 复制(file_id,标题=无,copy_permissions=False) https://gspread.readthedocs.io/en/latest/api.html#gspread.Client.copy

Added:

  • 您想要复制 Google 电子表格中的一张工作表。

我可以像上面那样理解。为此,示例脚本如下。

示例脚本:

client = gspread.authorize(credentials)
client.copy("18Qk5bzuA7JOBD8CTgwvKYRiMl_35it5AwcFG2Bi5npo", title="timcard2", copy_permissions=True)

ss = client.open("timcard2")
ss.share("[email protected] /cdn-cgi/l/email-protection", perm_type='user', role='writer')

delete_sheets = ["Sheet2", "Sheet3", "Sheet4"]  # Please set the sheet names you want to delete.
for s in delete_sheets:
    ss.del_worksheet(ss.worksheet(s))
  • 在此示例中,从复制的电子表格中删除“Sheet2”、“Sheet3”、“Sheet4”工作表。

参考:

  • del_worksheet(工作表) https://gspread.readthedocs.io/en/latest/api.html#gspread.models.Spreadsheet.del_worksheet
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Gspread如何复制sheet 的相关文章

随机推荐

  • jQuery 验证规则和消息 [关闭]

    很难说出这里问的是什么 这个问题是含糊的 模糊的 不完整的 过于宽泛的或修辞性的 无法以目前的形式得到合理的回答 如需帮助澄清此问题以便重新打开 访问帮助中心 help reopen questions 我在设置 jQuery 验证的规则和
  • 如何以编程方式播放 16 位 pcm 数组 [关闭]

    Closed 这个问题不符合堆栈溢出指南 help closed questions 目前不接受答案 我有一个包含 16 位 pcm 值的短 数组 我希望能够在不添加任何标题 也不将任何文件保存到内存的情况下播放它 我知道我可能需要一个提供
  • 连接 3 三张表

    我有这个图表应该可以解释我的情况 我需要一些关于连接 3 个表的帮助 我不知道如何做这种事情 因此 我可以通过执行以下操作来经历一段检索记录的 while 循环 img src alt Album AlbumID 使用内部联接 http w
  • JavaScript 动画平滑滚动

    默认情况下 当您有这样的片段链接时 a href some url some fragment some text a 浏览器立即向下滚动到该片段 我该如何编程才能使用标准 JS 顺利地向下移动到该片段 这是一个例子 Example htt
  • 如何在 WordPress HTML 中包含“onclick”对象

    我正在尝试将 onclick 对象添加到触发事件的单站点 即而不是多站点 WordPress 中的页面 代码是 a href Send a voice message a 当尝试保存代码时 WordPress 会删除 onclick 对象
  • 列出 JSON 的所有键和值

    假设我有一些如下所示的 JSON items item id 0001 type donut name Cake ppu 0 55 batters batter
  • 如何使用xquery查找节点并向其添加子节点?

    是否可以使用xpath xquery查询特定的xml节点 然后向其导入 添加子节点 示例 代码取自http codepad org gJ1Y2LjM http codepad org gJ1Y2LjM 这是在类似的问题中提出的 但不相同 1
  • 结构差异 sudo() run('sudo 命令')

    我想知道函数之间有什么区别sudo 和函数run sudo u user smth 文档上有 sudo 在所有运行方式上都是相同的 除了它总是换行 调用 sudo 程序中的给定命令以提供超级用户 特权 但有几次 sudo cmd 提示我输入
  • Apple 的 SKPaymentTransactionStateDeferred 警报消息流是什么?

    在 iOS8 中 Apple 为 IAP 交易引入了新的 SKPaymentTransactionStateDeferred 状态 例如 参见https developer apple com library ios technotes t
  • 将下拉列表与字典绑定

    我将字典绑定到下拉列表 举例来说 我的字典中有以下项目 Test1 123 Test2 321 我希望下拉文本采用以下格式 Test1 Count 123 Test2 Count 321 我沿着以下路径走 但没有运气 MyDropDown
  • 在 matplotlib 中的极坐标图上移动径向刻度标签

    From matplotlib 示例 http matplotlib org examples pylab examples polar demo html import numpy as np import seaborn as sbs
  • Spotify 登录错误 INVALID_CLIENT:无效的重定向 URI android

    我正在制作一个包含 Spotify 集成的应用程序 我点击了此链接https developer spotify com technologies spotify android sdk tutorial https developer s
  • 如何改变HTML5视频的播放速度?

    如何更改 HTML5 中的视频播放速度 我查过视频标签的属性 https www w3schools com html html5 video asp在 w3school 但无法做到这一点 根据这个网站 http www chipwreck
  • 通过docker使用Selenium python库,Chrome报错无法启动:异常退出

    我正在尝试使用以下命令运行一些 python 脚本selenium我从基于 miniconda anaconda 的 docker 容器中获取了库 但我不断收到此错误 selenium common exceptions WebDriver
  • 如果在等待“read -s”时中断,在子进程中运行 bash 会破坏 tty 的标准输出吗?

    正如 Bakuriu 在评论中指出的那样 这基本上与BASH 输入期间按 Ctrl C 会中断当前终端 https stackoverflow com questions 31808863 bash ctrlc during input b
  • PHP:如何检查总数。 URL 中的参数?

    我正在使用 REQUEST 检索参数 有没有办法找到总数 URL 中的参数 而不是检索每个参数然后进行计数 这将为您提供总数 分隔的 URL 查询参数 count explode SERVER QUERY STRING 如果您只想要唯一的参
  • 如何知道用户是否在 Android 应用程序中输入了错误的密码(锁定屏幕)

    我正在开发一个 Android 应用程序 如果用户在 Android 锁定屏幕中输入错误的密码 则必须完成其中一项活动 例如 如果用户输入错误的密码 则会发送电子邮件 我将不胜感激任何帮助 提前致谢 Kshitij 锁屏在完全沙箱环境中运行
  • 如何避免连接两个表时重复

    Student Table SID Name 1 A 2 B 3 C Marks Table id mark subject 1 50 physics 2 40 biology 1 50 chemistry 3 30 mathematics
  • C 中“complex”的默认类型

    根据我读过的文档 C99 和更高版本的支持float complex double complex and long double complex作为复杂类型 但是 此代码在使用时编译时不会发出警告gcc Wall Wextra inclu
  • Gspread如何复制sheet

    在 Stackoverflow 上进行谷歌搜索和搜索后 我想我找不到有关如何复制现有工作表 现有模板工作表 并将其保存到另一个工作表中的指南 根据文档 有重复表 https gspread readthedocs io en latest