简体   繁体   中英

Update cell color using Google Sheets Node.js API

I'm trying to change the color of cells/text color/borders in a google sheet using Node.js. I have some data, and I want to put that data in the sheet and highlight certain rows based on conditions. The putting in data part works when I use sheets.spreadsheets.values.update . As a basic test for highlighting, I tried just changing some attributes of cell A1 like this:

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);
    });

but all that happens is that the formatting on cell A1 gets reset (I manually set it to bold, and it un-set the boldness). The value does not get set to 'hello please' .

How do you set the color of a cell (or range of cells)?

How about the following suggestions?

Suggestion 1:

Modify fields in your request body as follows:

From:

fields: '*',

To:

fields: 'effectiveValue,effectiveFormat',

Suggestion 2:

Modify fields , effectiveValue and effectiveFormat in your request body as follows. ( effectiveValue and effectiveFormat are modified to userEnteredValue and userEnteredFormat , respectively.)

From:

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

To:

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

Reference:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM