能否使用Google Sheets API的updateCells写入整行数据?
问题解决:使用updateCells写入Google Sheets整行数据
完全可以用updateCells实现整行数据写入,你当前的问题是代码中rows的结构错误:单个行对象里重复定义了values属性,后面的values会覆盖前面的,最终只有最后一组值(也就是'Completed')被写入。
修正后的body结构应该把整行的所有单元格值放在同一个values数组中,每个元素对应一列的单元格数据:
body = { 'requests': [ { 'updateCells': { 'rows': [{ 'values': [ { 'userEnteredValue': { 'stringValue': 'ID' } }, { 'userEnteredValue': { 'stringValue': 'Name' } }, { 'userEnteredValue': { 'stringValue': 'Surname' } }, { 'userEnteredValue': { 'stringValue': 'Phone Number' } }, { 'userEnteredValue': { 'stringValue': 'Whatsapp Number' } }, { 'userEnteredValue': { 'stringValue': 'Email' } }, { 'userEnteredValue': { 'stringValue': 'Location' } }, { 'userEnteredValue': { 'stringValue': 'Date' } }, { 'userEnteredValue': { 'stringValue': 'Completed' } } ] }], 'fields': 'userEnteredValue', 'range': { "sheetId": 0, "startRowIndex": 0, "startColumnIndex": 0, "endRowIndex": 1, "endColumnIndex": 9 } } } ] }
另外补充两个细节:
range里补充endRowIndex:1,明确指定只更新第一行(索引从0开始),避免误操作影响其他行- 你的
fetch请求参数格式需要调整为标准对象结构,修正后代码如下:
fetch(`${endpoint}/${id}/:batchUpdate`, { method: method, headers: { 'Accept': 'application/x-www-form-urlencoded', 'Authorization': `Bearer ${access_token}`, 'Content-Type': 'application/json' }, body: JSON.stringify(body) })
调整后就能一次性写入整行的8个单元格数据了。
内容的提问来源于stack exchange,提问作者Bret Joseph
相关产品推荐
相关产品推荐

