【问题标题】:Update cell color using Google Sheets Node.js API使用 Google Sheets Node.js API 更新单元格颜色
【发布时间】:2020-11-06 20:40:15
【问题描述】:

我正在尝试使用 Node.js 更改谷歌表格中单元格/文本颜色/边框的颜色。我有一些数据,我想将这些数据放在工作表中,并根据条件突出显示某些行。当我使用sheets.spreadsheets.values.update 时,输入数据部分有效。作为突出显示的基本测试,我尝试像这样更改单元格 A1 的一些属性:

sheets.spreadsheets
    .batchUpdate({
        spreadsheetId: masterOrderSheetID,
        resource: {
            requests: [
                {
                    updateCells: {
                        start: {
                            sheetId: 0,
                            rowIndex: 0,
                            columnIndex: 0,
                        },
                        fields: '*',
                        rows: [
                            {
                                values: [
                                    {
                                        effectiveValue: {
                                            stringValue: 'hello please',
                                        },
                                        effectiveFormat: {
                                            backgroundColor: {
                                                red: 1,
                                                green: 0,
                                                blue: 0,
                                                alpha: 0.5,
                                            },
                                            backgroundColorStyle: {
                                                rgbColor: {
                                                    red: 1,
                                                    green: 0,
                                                    blue: 0,
                                                    alpha: 0.5,
                                                },
                                            },
                                            textRotation: {
                                                angle: 45,
                                            },
                                        },
                                    },
                                ],
                            },
                        ],
                    },
                },
            ],
            includeSpreadsheetInResponse: true,
            responseIncludeGridData: true,
        },
    })
    .then((response) => {
        console.log(response);
    })
    .catch((error) => {
        console.error(error);
    });

但所发生的只是单元格 A1 上的格式被重置(我手动将其设置为粗体,然后取消设置粗体)。该值未设置为'hello please'

如何设置单元格(或单元格范围)的颜色?

【问题讨论】:

    标签: node.js google-sheets google-api


    【解决方案1】:

    以下建议怎么样?

    建议一:

    在您的请求正文中修改fields,如下所示:

    发件人:

    fields: '*',
    

    收件人:

    fields: 'effectiveValue,effectiveFormat',
    

    建议二:

    在您的请求正文中修改fieldseffectiveValueeffectiveFormat,如下所示。 (effectiveValueeffectiveFormat分别修改为userEnteredValueuserEnteredFormat。)

    发件人:

    fields: '*',
    rows: [
        {
            values: [
                {
                    effectiveValue: {
                        stringValue: 'hello please',
                    },
                    effectiveFormat: {
    

    收件人:

    fields: 'userEnteredValue,userEnteredFormat"',
    rows: [
        {
            values: [
                {
                    userEnteredValue: {
                        stringValue: 'hello please',
                    },
                    userEnteredFormat: {
    

    参考:

    【讨论】:

    • 感谢田池的帮助。我将fields 部分保留为'*',但我将effectiveFormat 更改为userEnteredFormat 并且它起作用了。我遇到的另一个问题是我在没有await.then() 的情况下紧接着调用sheets.spreadsheets.values.update(设置一些值)和sheets.spreadsheets.batchUpdate(设置一些颜色)。因此,在进行这些更改后,我可以正确设置颜色 :)
    • @nullromo 感谢您的回复。从您的回复中,我了解到您的第一个问题已解决。关于你的第二个问题,我不得不为我糟糕的英语水平道歉。不幸的是,我无法理解它。我可以问你关于它的细节和你的目标吗?如果可以,您能否提供您期望的输出示例图像?通过这个,我想尝试了解他们。你能合作解决你的第二个问题吗?我再次为我糟糕的英语水平深表歉意。
    • 哦,只有一个问题,那就是如何设置颜色。它不适用于effectiveFormat,但它适用于userEnteredFormat。我的用例所需的其余部分我可以自己弄清楚。再次感谢您的帮助;考虑要回答的问题。无需为您的英语水平道歉。我明白你的意思:)
    • @nullromo 感谢您的回复。如果我的回答有用,我很高兴。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多