使用Python&将工作表移动到特定位置Google Sheets API [英] Move a sheet to a particular position using Python & the Google Sheets API

本文介绍了使用Python&将工作表移动到特定位置Google Sheets API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过python使用Google sheet API.到目前为止,我已经成功完成了一些任务.我的问题是,例如,在一个特定的工作表中,按

I am trying to use the Google sheet API via python. So far I have been successful in performing some tasks. My problem is for example I have 4 sheets in a particular worksheet in the order of

[sheet1][sheet2][sheet3][sheet4]. 

我想要的是操纵纸张的位置.例如,工作表4现在将位于第二位置.所以现在,工作表的位置将变为

What I want is to manipulate the positions of the sheets. For example, sheet 4 would now be located at the second position. So now, the positions of the sheets would become

[sheet1][sheet4][sheet2][sheet3]

有没有办法在python中做到这一点?

Is there a way to do this in python?

谢谢!

推荐答案

是的,您可以使用Python或 Google表格API .假设您不知道在哪里 sheet4在哪里,但想将其移动到如图所示的第二个插槽中.

Yes, you can do this in Python or any other language supported by the Google APIs Client Libraries. Regardless of which language, you need to use the latest Google Sheets API. Let's say you don't know where sheet4 is but want to move it to the 2nd slot as you've diagrammed.

这是您的步骤,假设SHEETS是您的服务端点,并且FILE_ID是Google云端硬盘中的表格文件的ID:

Here are your steps, assuming SHEETS is your service endpoint, and FILE_ID is the ID of the Sheets file in your Google Drive:

  1. 通过获取表格文件数据来查找要移动的表格的ID:rsp = SHEETS.spreadsheets().get()
  2. 循环遍历每张纸,即for sheet in rsp['sheets']并匹配sheet['properties']['title'] == 'sheet4'
  3. 现在通过将其索引设置为2来更新那个工作表的属性,如下所示.
  1. Find the ID of the sheet you want to move by getting your Sheets file data: rsp = SHEETS.spreadsheets().get()
  2. Cycle through each sheet, i.e., for sheet in rsp['sheets'] and match sheet['properties']['title'] == 'sheet4'
  3. Now update the properties of that sheet by setting its index to 2 with something like the request below.

这是最后一部分的代码:

Here's the code for the last part:

reqs = {'requests': [
    # reorder sheet4 to index 2
    {'updateSheetProperties': {
        'properties': {
            'sheetId': sheet['properties']['sheetId'],
            'index': 2
        }
    }}
]}
SHEETS.spreadsheets().batchUpdate(
    spreadsheetId=FILE_ID, body=reqs).execute()

第2张&进行此更改后,第3步应会滑过.希望这会有所帮助!

Sheets 2 & 3 should slide over after you've made this change. Hope this helps!

如果您已经试用了文档中的示例,并且想通过看一些将Sheets API与Python结合使用的真实世界"示例来了解更多信息,那么我制作了一些视频,这些视频可能会很有用:

If you've already played around with the samples in the docs and want to learn more by seeing some "real-world" examples of using the Sheets API with Python, I've made a couple of videos that may be useful:

  • Migrating SQL data to a Sheet (code deep dive post)
  • Formatting text using the Sheets API (code deep dive post)
  • Generating slides from spreadsheet data (code deep dive post)
  • Those and others in the Sheets API video library

这篇关于使用Python&将工作表移动到特定位置Google Sheets API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆