Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google sheets 你能用谷歌电子表格API更新单元格的样式而不覆盖值吗?_Google Sheets_Google Sheets Api - Fatal编程技术网

Google sheets 你能用谷歌电子表格API更新单元格的样式而不覆盖值吗?

Google sheets 你能用谷歌电子表格API更新单元格的样式而不覆盖值吗?,google-sheets,google-sheets-api,Google Sheets,Google Sheets Api,我正在使用谷歌电子表格API来寻找一个单元格的背景颜色 通过使用batchUpdate和userEnteredFormat这很有效。下面的代码是一个简化版本,它将使单元格的背景色变为红色 const request = { spreadsheetId: 'xxx', resource: { [{ updateCells: { range: { ... }, fields: '*', r

我正在使用谷歌电子表格API来寻找一个单元格的背景颜色

通过使用batchUpdateuserEnteredFormat这很有效。下面的代码是一个简化版本,它将使单元格的背景色变为红色

const request = {
  spreadsheetId: 'xxx',
  resource: {
    [{
      updateCells: {
        range: {
          ...
        },
        fields: '*',
        rows: [
          {
            values: [
              {
                userEnteredFormat: {
                  backgroundColor: { red: 1, green: 0, blue: 0 },
                },
              },
            ],
          },
        ],
      },
    }],
  },
};

const result = await sheets.spreadsheets.batchUpdate(request);
问题是,这也会覆盖单元格值

我本以为只有在设置userEnteredValue时才会覆盖该值,但它始终会覆盖该值


如何更新单元格的样式而不覆盖其值?

我认为您的请求正文有两个修改点,如下所示

修改脚本:
const请求={
电子表格ID:spreadsheetId,
资源:{

requests:[//谢谢,这确实很有用。在简化示例时,我忘记了requests属性。@Wannes感谢您的回复。我很高兴您的问题得到了解决。
const request = {
  spreadsheetId: spreadsheetId,
  resource: {
    requests: [  // <--- Modified
      {
        updateCells: {
          range: {
            ...
          },
          fields: "userEnteredFormat.backgroundColor",  // <--- Modified
          rows: [
            {
              values: [
                {
                  userEnteredFormat: {
                    backgroundColor: { red: 1, green: 0, blue: 0 },
                  },
                },
              ],
            },
          ],
        },
      },
    ],
  },
};