简体   繁体   English

获取 gspread 以使用相同的数据更新多个电子表格文件

[英]Getting gspread to update multiple spreadsheet files with the same data

As above I'm trying to update more than one sheet file with the same information using the sheet IDs as the identifier.如上所述,我尝试使用工作表 ID 作为标识符更新多个具有相同信息的工作表文件。 I''ve attempted to achieve this with for loops with no success.我试图用 for 循环来实现这一点,但没有成功。 Some of the basic code that I'm trying to achieve this with is:我试图用它来实现的一些基本代码是:

conn = gspread.authorize(credentials)
sheets = ['sheetid1', 'sheetid2']
worksheet_list = conn.open_by_key(sheets).worksheet("Rack Layout")
worksheet_list.update_acell('Q1', 'some cell value')

So if I define 'sheets' as one sheetid the updates work fine, however if I define 'sheets' as more than one sheet I get the error.因此,如果我将 'sheets' 定义为一个 sheetid,则更新工作正常,但是如果我将 'sheets' 定义为多个工作表,则会出现错误。 I know this is really basic and I think the issue is its trying to open the full list (ie both sheetids) on the same line, rather than saying 'run this line for one sheetID and then the next'.我知道这真的很基本,我认为问题在于它试图在同一行上打开完整列表(即两个 sheetid),而不是说“先为一个 sheetID 运行此行,然后再运行下一个”。 I think the way to achieve this is a for loop, but I've yet to get this to work with a for loop.我认为实现这一点的方法是 for 循环,但我还没有让它与 for 循环一起工作。 I know this is a nube question, but I've been trying for a while now, and from my searching I haven't found any other forum posts about getting gspread to do this.我知道这是一个 nube 问题,但我已经尝试了一段时间,从我的搜索中我没有找到任何其他关于让 gspread 做到这一点的论坛帖子。

  • You have 2 Google Spreadsheets.您有 2 个 Google 电子表格。
    • 2 Google Spreadsheets have the sheet of "Rack Layout", respectively. 2 谷歌电子表格分别有“机架布局”表。
  • You want to put the value of "some cell value" to the cell "Q1" in the sheet of "Rack Layout" for each Spreadsheet.您想将“某个单元格值”的值放在每个电子表格的“机架布局”表中的单元格“Q1”中。
  • You want to achieve this using gspread with python.您想使用 gspread 和 python 来实现这一点。
  • You have already been able to get and put values for Spreadsheet using Sheets API.您已经能够使用 Sheets API 获取和放置电子表格的值。

If my understanding is correct, how about this answer?如果我的理解是正确的,这个答案怎么样? Please think of this as just one of several possible answers.请将此视为几种可能的答案之一。

In this modified script, the value is put to each Spreadsheet using the for loop.在这个修改后的脚本中,使用 for 循环将值放入每个电子表格。

Modified script:修改后的脚本:

conn = gspread.authorize(credentials)
sheets = ['sheetid1', 'sheetid2']
for sheet in sheets:  # Added
    worksheet_list = conn.open_by_key(sheet).worksheet("Rack Layout")
    worksheet_list.update_acell('Q1', 'some cell value')

If I misunderstood your question and this was not the direction you want, I apologize.如果我误解了您的问题并且这不是您想要的方向,我深表歉意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM