【问题标题】:Inserting Cells with Gdata, Python Google Spreadsheet Api client使用 Gdata 插入单元格,Python Google 电子表格 Api 客户端
【发布时间】:2012-10-17 10:17:36
【问题描述】:

我正在尝试更新或将单元格插入到谷歌电子表格中,这是我第一次使用谷歌 api 和 python,所以我可能做了一些点头的事情.. 但我一辈子都看不到它。

当前,当我注释掉“batchRequest.AddInsert(newCell)”时,我的代码会更新现有单元格但不插入新单元格(空白单元格保持空白) 但是当它离开时,我得到一个返回的状态码 500 和一堆 xml,这没什么帮助。

代码(部分)

def setRowEntries(self, cohort_key=None):
  cohort = self.GetCohort(cohort_key)

  gd_client = gdata.spreadsheet.service.SpreadsheetsService()
  gd_client.email = email
  gd_client.password = password
  gd_client.source = 'YOUR_APP_NAME'

  # LOGIN
  try:
    # log in
    gd_client.ProgrammaticLogin()
  except socket.sslerror, e:
    logging.error('Spreadsheet socket.sslerror: ' + str(e))
    return False

  # KEY FOR SHEET
  key = '0At**************************Gc'

  # LOOKUP FOR WORKSHEET KEY
  GID_TABLE = {
    'android_users_by_week_by_registration' : 'od6'
  }

  wksht_id =''
  wksht_id = GID_TABLE[cohort_key]

  #  create a cells feed and batch request
  cells = gd_client.GetCellsFeed(key, wksht_id)
  batchRequest = gdata.spreadsheet.SpreadsheetsCellsFeed()

  #TODO SET TITLE

  #resize worksheet
 # cells.col_count = len(cohort[0])
 # cells.row_count = len(cohort)

  rowcursor = 0
  for myrow in cohort:
    colcursor = 0
    #print ("row ::"+str(myrow))
    for mycell in myrow:
      #print ("cell::"+str(mycell))
      found = 0
      #print ("try and place"+str(rowcursor)+','+str(colcursor))
      for myentry in cells.entry:
        if ((int(myentry.cell.row) == int(rowcursor+1)) and (int(myentry.cell.col) == int(colcursor+1))):
          print "updating  "+myentry.cell.text+" to "+str(mycell)
          myentry.cell.inputValue = str(mycell)
          batchRequest.AddUpdate(myentry)
          found = 1

      if not found:
        print "inserting "+str(mycell)+" at Cell "+ str(rowcursor+1)+'_'+str(colcursor+1)
        newCell = gdata.spreadsheet.SpreadsheetsCell()
        newCell.cell = gdata.spreadsheet.Cell(inputValue=str(mycell), text=None, row=str(rowcursor+1), col=str(colcursor+1))
        print newCEll.inpu
        batchRequest.AddInsert(newCell)# the broken part
      colcursor = colcursor + 1
    rowcursor = rowcursor + 1

  updated = gd_client.ExecuteBatch(batchRequest, cells.GetBatchLink().href)

  if updated:
    print "Sucessfull!"+str(updated)
  else:
    print "Failed!"

错误(部分)

type="text">Internal Error</ns0:content><ns1:id xmlns:ns1="http://schemas.google.com/gdata/batch">1626</ns1:id><ns0:title type="text">Error</ns0:title><ns1:status code="500" reason="Internal Error" xmlns:ns1="http://schemas.google.com/gdata/batch" /><ns1:operation type="insert" xmlns:ns1="http://schemas.google.com/gdata/batch" /><ns0:updated>2012-10-17T09:39:18.946Z</ns0:updated></ns0:entry><ns0:entry><ns0:id>1627</ns0:id><ns0:content type="text">Internal Error</ns0:content><ns1:id xmlns:ns1="http://schemas.google.com/gdata/batch">1627</ns1:id><ns0:title type="text">Error</ns0:title><ns1:status code="500" reason="Internal Error" xmlns:ns1="http://schemas.google.com/gdata/batch" /><ns1:operation type="insert" xmlns:ns1="http://schemas.google.com/gdata/batch" /><ns0:updated>2012-10-17T09:39:18.946Z</ns0:updated></ns0:entry><ns0:entry><ns0:id>1628</ns0:id><ns0:content type="text">Internal Error</ns0:content><ns1:id xmlns:ns1="http://schemas.google.com/gdata/batch">1628</ns1:id><ns0:title type="text">Error</ns0:title><ns1:status code="500" reason="Internal Error" xmlns:ns1="http://schemas.google.com/gdata/batch" /><ns1:operation type="insert" xmlns:ns1="http://schemas.google.com/gdata/batch" /><ns0:updated>2012-10-17T09:39:18.946Z</ns0:updated></ns0:entry><ns0:entry><ns0:id>1629</ns0:id><ns0:content type="text">Internal Error</ns0:content><ns1:id xmlns:ns1="http://schemas.google.com/gdata/batch">1629</ns1:id><ns0:title type="text">Error</ns0:title><ns1:status code="500" reason="Internal Error" xmlns:ns1="http://schemas.google.com/gdata/batch" /><ns1:operation type="insert" xmlns:ns1="http://schemas.google.com/gdata/batch" /><ns0:updated>2012-10-17T09:39:18.946Z</ns0:updated></ns0:entry><ns0:entry><ns0:id>1630</ns0:id><ns0:content 

我发现 google api 文档很难理解,示例很少而且范围太窄。如果你能让我知道我做错了什么那就太好了

【问题讨论】:

    标签: python api google-sheets-api gdata


    【解决方案1】:

    在 chrome 中查看 xml 时原因变得很清楚,批量不支持插入。看起来新单元格必须通过工作表提要进入。

    【讨论】:

    • 如果您厌倦了使用官方 API,请查看我一年前破解的 library。我试图让 API 尽可能简单。
    猜你喜欢
    • 2012-07-10
    • 1970-01-01
    • 1970-01-01
    • 2012-05-23
    • 2011-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多