使用python覆盖Google工作表中的数据 [英] overwrite data in google sheet using python

查看:204
本文介绍了使用python覆盖Google工作表中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图写到Google工作表,但我想这样做,但我不想修改和添加越来越多的数据,而是希望将数据覆盖(在同一位置删除并替换),正在使用python,尽管它并不重要... R-PI.

I have been trying to write to google sheets, I have been able to do so, but instead of amending and adding more and more data, I would like the data to be overwritten(erased and replaced in the same location) I'm using python and though it's not important...a R-PI.

# import many libraries
# -*- coding: utf-8 -*-
from __future__ import print_function  
from googleapiclient.discovery import build  
from httplib2 import Http  
from oauth2client import file, client, tools  
from oauth2client.service_account import ServiceAccountCredentials  
#import bme280  
import datetime

# My Spreadsheet ID ... See google documentation on how to derive this
MY_SPREADSHEET_ID = '...............someID.....................'

def update_sheet(sheetname, my_list):  
    """update_sheet method:
       appends a row of a sheet in the spreadsheet with the 
       the latest temperature, pressure and humidity sensor data
    """
    # authentication, authorization step
    SCOPES = 'https://www.googleapis.com/auth/spreadsheets'
    creds = ServiceAccountCredentials.from_json_keyfile_name( 
            'client_secret.json', SCOPES)
    service = build('sheets', 'v4', http=creds.authorize(Http()))

    # Call the Sheets API, append the next row of sensor data
    # values is the array of rows we are updating, its a single row

    values = [ [ str(datetime.datetime.now()), my_list ] ]
    body={'values': my_list}
    result = service.spreadsheets().values().append(
    spreadsheetId=MY_SPREADSHEET_ID,
    range = 'PCEM SHT.1' +'!A2:A7', 
    valueInputOption = 'RAW',
    body=body).execute()

def main():  
    my_list = [['a', 'b'], ['c', 'd'], ['e', 'f'], ['g', 'h']]
    update_sheet("PCEM SHT.1", my_list)

if __name__ == '__main__':  
    main()

推荐答案

使用update()代替service.spreadsheets().values().append()中的append()方法.更多信息是文档中可用.

Instead of the append() method in service.spreadsheets().values().append() use update(). More info is available in documentation.

这篇关于使用python覆盖Google工作表中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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