将值表添加到Google工作表(python)

时间:2017-11-14 16:29:01

标签: python google-sheets

#Write to sheet
subjects = ['Maths', 'Physics', 'Geography', 'Biology',
            'Chemistry', 'ICT', 'Travel and Tourism', 'Computer Science',
            'History', 'LINC', 'Design and Technology', 'Art',
            'English Literature', 'Welsh', 'MFL', 'Music',
            'Business','Applied Science', 'Medical Science', 'Media']
values =[]
import random
random.shuffle(subjects)

chunks = [subjects[x:x+5] for x in range(0, len(subjects), 5)]
I = chunks[0]
c = 0
for element in I:
    temp = str(element)
    print(temp)
    values.append(temp)
    print(values)
body = {
    'values': values,
    'majorDimension':'COLUMNS'

}
result = service.spreadsheets().values().update(
    spreadsheetId=spreadsheetId, range="Data!A1:D5",
    valueInputOption='USER_ENTERED',body=body).execute()

运行它会基本上随机移动一些列表元素,然后将它们拆分成较小的列表,这样我就可以逐列添加它们,我的范围目前是在Google工作表上的A1:D5但是我尝试过只使用A1和a单个列表元素,我得到相同的错误,例如只有"地理"附加到列表

我的错误如下:

googleapiclient.errors.HttpError: <HttpError 400 when requesting https://sheets.googleapis.com/v4/spreadsheets/1DIP0UqEuS8mcEgAiKnFXaSsyqiUq2LXkGa7VmAJS4S8/values/Data%21A1?valueInputOption=USER_ENTERED&alt=json returned "Invalid value at 'data.values[0]' (type.googleapis.com/google.protobuf.ListValue), "English Literature"">

2 个答案:

答案 0 :(得分:0)

似乎values是一维列表。在spreadsheets().values().update(),列表必须是二维列表。那么下面的修改呢?

模式1:

如果你想将5个值导入&#34; A1:A5&#34;,你可以使用它。

来自:

body = {
    'values': values,
    'majorDimension':'COLUMNS'
}

致:

body = {
    'values': [values], # Modified
    'majorDimension':'COLUMNS'
}

模式2:

如果您想将5个值导入&#34; A1:E1&#34;,您可以使用它。

来自:

for element in I:
    temp = str(element)
    print(temp)
    values.append(temp)
    print(values)
body = {
    'values': values,
    'majorDimension':'COLUMNS'
}
result = service.spreadsheets().values().update(
    spreadsheetId=spreadsheetId, range="Data!A1:D5",
    valueInputOption='USER_ENTERED',body=body).execute()

致:

for element in I:
    temp = str(element)
    print(temp)
    values.append([temp]) # Modified
    print(values)
body = {
    'values': values,
    'majorDimension':'COLUMNS'
}
result = service.spreadsheets().values().update(
    spreadsheetId=spreadsheetId, range="Data!A1:E1", # Modified
    valueInputOption='USER_ENTERED',body=body).execute()

如果我误解了你的问题,我很抱歉。

答案 1 :(得分:0)

subjects = ['Maths', 'Physics', 'Geography', 'Biology',
            'Chemistry', 'ICT', 'Travel and Tourism', 'Computer Science',
            'History', 'LINC', 'Design and Technology', 'Art',
            'English Literature', 'Welsh', 'MFL', 'Music',
            'Business','Applied Science', 'Medical Science', 'Media']
t = ['Maths','Chemistry']
E = 0
Alphabet = ['A','B','C','D','E']
tempList = []
import random
random.shuffle(subjects)

chunks = [subjects[x:x+5] for x in range(0, len(subjects), 5)]
I = chunks[0]
c = 0
#print(chunks[0])
for chunk in chunks:
    R = Alphabet[E]
    print(chunk)
    c = c+1
    values = [[chunk[0]],[chunk[1]],[chunk[2]],[chunk[3]],[chunk[4]],]
    body = {
    'values': values,
    'majorDimension':'ROWS'

    }
    result = service.spreadsheets().values().update(
        spreadsheetId=spreadsheetId, range="Data!"+R+"2:Z",
        valueInputOption='RAW',body=body).execute()
    E = E+1