1

I'm starting to develop with the Google Sheets api after checking the documentation there are several things that I don't understand or I can't find.

This is the endpoint I am testing:

spreadsheets.values.batchUpdate

https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/batchUpdate

What I am trying to do is format cells to plain text from the API, the same thing I do from the spreadsheet by clicking on Format > Number > Plain text

How should I create the JSON from the API to apply the same format to an entire column?

This is what happens when I format the cell to Plain Text from the sheet itself:

  1. This is the cell I am going to format:

enter image description here

  1. This is what I do:

Format > Number > Plain text

  1. This is the result:

enter image description here

This is what happens when I format the same cell to Plain Text from the google API:

  1. This is the JSON that I apply:

         "requests": [
           {
             "repeatCell": {
               "range": {
                 "sheetId": 3,
                 "startColumnIndex": 1,
                 "endColumnIndex": 2,
                 "startRowIndex": 1,
                 "endRowIndex": 2
               },
               "cell": {
                 "userEnteredFormat": {
                   "numberFormat": {
                     "type": "TEXT"
                   }
                 }
               },
               "fields": "userEnteredFormat.numberFormat"
             }
           }
         ]
       }```
    
    
  2. This is the result:

enter image description here

My problem: How can I do from the API the same thing that I do directly from the sheet?

Tanaike
  • 181,128
  • 11
  • 97
  • 165
synffryd
  • 171
  • 2
  • 3
  • 10
  • What programming language are you using have you tried following one of the official tutorials that uses one of the official client libraries? – Linda Lawton - DaImTo Mar 05 '22 at 12:07
  • @DaImTo I am developing in NodeJS. I just updated the question with a practical example of what I'm doing and what happens – synffryd Mar 05 '22 at 12:35

1 Answers1

2

I had had the same situation as you. At that time, as a workaround, I used the following flow.

  1. Retrieve values from the sheet as the formatted values.
  2. Put the values on the sheet as the string values.

When this workaround is reflected in a script, it becomes as follows.

Sample script:

const spreadsheetId = "###"; // Please set the Spreadsheet ID.
const sheetName = "Sheet1"; // Please set the sheet name.
const sheets = google.sheets({ version: "v4", auth }); // Please use your authorization script.

const values = await sheets.spreadsheets.values.get({
  spreadsheetId,
  range: sheetName,
  valueRenderOption: "FORMATTED_VALUE",
});
const res = await sheets.spreadsheets.values.update({
  spreadsheetId,
  range: sheetName,
  valueInputOption: "RAW",
  resource: { values: values.data.values },
});

Result:

When this script is used, the following result is obtained.

From:

enter image description here

To:

enter image description here

Note:

  • This sample script uses a sheet. If you want to use the specific range, please modify range.

  • In this script, the number format is changed to Automatic. If you want to change this to Plain text, please use the following script instead of the above one.

      const spreadsheetId = "###"; // Please set the Spreadsheet ID.
      const sheetName = "Sheet1"; // Please set the sheet name.
      const sheets = google.sheets({ version: "v4", auth }); // Please use your authorization script.
      const sheetId = 0; // Please set the sheet ID of "sheetName".
    
      const values = await sheets.spreadsheets.values.get({
        spreadsheetId,
        range: sheetName,
        valueRenderOption: "FORMATTED_VALUE",
      });
      const requests = values.data.values.map((r, i) =>
        r.map((c, j) => ({
          updateCells: {
            range: {
              sheetId: sheetId,
              startRowIndex: i,
              endRowIndex: i + 1,
              startColumnIndex: j,
              endColumnIndex: j + 1,
            },
            rows: [
              {
                values: [
                  {
                    userEnteredFormat: {
                      numberFormat: {
                        type: "TEXT",
                      },
                    },
                    userEnteredValue: {
                      stringValue: c,
                    },
                  },
                ],
              },
            ],
            fields: "userEnteredFormat.numberFormat,userEnteredValue",
          },
        }))
      );
      const res = await sheets.spreadsheets.batchUpdate({
        spreadsheetId,
        resource: { requests },
      });
    

References:

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • If I understand it correctly, what this last script does is obtain the values of that range and insert them again with another format. Isn't there a way to format that data straight to plain text? – synffryd Mar 07 '22 at 09:58
  • @synffryd Thank you for replying. I apologize for my poor skill. About `If I understand it correctly, what this last script does is obtain the values of that range and insert them again with another format. Isn't there a way to format that data straight to plain text?`, I proposed this workaround because there are no methods for directly achieving your goal using Sheets API. This is due to my poor skill. I deeply apologize for this. – Tanaike Mar 07 '22 at 11:53
  • @synffryd For example, when `setNumberFormat("@")` of Spreadsheet service which is not Sheets API is used, your goal can be directly achieved. But this doesn't use Sheets API. So I didn't propose it. I deeply apologize for this again. – Tanaike Mar 07 '22 at 11:53
  • Okay great, I was just curious, I'm new to this API thing. The code works fine but it seemed strange to me that there wasn't something more direct from the API, but it works, thanks – synffryd Mar 07 '22 at 15:56
  • @synffryd Thank you for replying. From `setNumberFormat("@")`, I guessed that in this case, the formatted value might be put by changing the number format in the internal server side. Because when `setNumberFormat("@")` is used, the value retrieved by Sheets API includes both the display value and numberFormat. On the other hand, when `@` is used as the number format using Sheets API, no display value is included. But, this is just my guess. – Tanaike Mar 08 '22 at 00:16