【问题标题】:Can Gspread insert new columns at location?Gspread 可以在位置插入新列吗?
【发布时间】:2017-04-05 04:28:38
【问题描述】:

我正在尝试使用 Gspread 在某个位置插入新列。 我找到了add_cols 方法,但它只插入电子表格的最后一列。

还有其他方法,例如:insert_rowsresizeappend_rows,但没有什么可以解决我的问题。我错过了什么吗? 谢谢你

【问题讨论】:

标签: python google-sheets gspread


【解决方案1】:

为此使用默认的批量更新 API

spreadsheetId=''
sheetId=''

sht = gc.open_by_key(spreadsheetId)

requests = []

requests.append({
      "insertDimension": {
        "range": {
          "sheetId": sheetId,
          "dimension": "COLUMNS",
          "startIndex": 2,
          "endIndex": 4
        },
        "inheritFromBefore": True
      }
    })

body = {
    'requests': requests
}

sht.batch_update(body)

【讨论】:

    【解决方案2】:

    不确定现在这是否对您有所帮助,但可能对其他人有所帮助。 另外,似乎Gspread还没有添加这个...... 如果您继续使用 gspread 站点包 - 您可以将其添加到“models.py”文件中,我在“def insert_row”上方执行此操作,但实际上您可以在任何地方执行此操作。

        def insert_col(self,values,index=1,value_input_option='RAW'):
            body = {
                "requests": [
                    {
                        "insertDimension": {
                            "range": {
                                "sheetId": self.id,
                                "dimension": "COLUMNS",
                                "startIndex": index - 1,
                                "endIndex": index,
                            }
                        }
                    }
                ]
            }
            self.spreadsheet.batch_update(body)
    
            range_label = absolute_range_name(self.title, 'A%s' % index)
    
            data = self.spreadsheet.values_update(
                range_label,
                params={'valueInputOption': value_input_option},
                body={'values': [values]},
            )
    
            return data
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-03
      • 2012-12-11
      • 1970-01-01
      • 1970-01-01
      • 2010-09-27
      相关资源
      最近更新 更多