Google Sheets API:插入格式化的文本 [英] Google Sheets API: insert formatted text

查看:132
本文介绍了Google Sheets API:插入格式化的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将粗体文本插入Google Spreadsheet.我有以下NodeJS代码,可成功插入文本.

I am trying to insert bolded text into Google Spreadsheet. I have the following NodeJS code that inserts the text successfully.

var body = {
 data: [{
   range: 'M2:M3',
   values: [["value1"],["value2"]]
 }],
 valueInputOption: 'USER_ENTERED',
};

var sheets = google.sheets('v4');
  sheets.spreadsheets.values.batchUpdate({
    auth: auth,
    spreadsheetId: SPREADSHEET_ID,
    resource: body
  }, function(err, response) {
    if (err) {
      console.log('The API returned an error: ' + err);
      return;
    }
  });

我抬头发现,为了将文本格式更改为粗体,需要使用以下代码.

I looked up and found that in order to change the text format to bold, the following code needs to be used.

requests: [
 {
   "format": {
     "textFormat": {
       "bold": true
      }
   }
 }
]

但是,我不明白此代码应该去哪里.尝试将requestsformat分别放在databatchUpdate内,但是不起作用.

However, I cannot understand where this code should go. Tried putting requests as well as format separately inside data and batchUpdate but it did not work.

推荐答案

该示例脚本如何?如果要同时更新单元格值和单元格格式,则可以使用sheets.batchUpdate.详细信息在此处.

How about this sample script? If you want to update cell values and cell format, simultaneously, you can use spreadsheets.batchUpdate. The detail information is here.

下面的示例将[["value1"],["value2"]]导入到'M2:M3',如您的脚本所示.例如,工作表ID为0.需要输入

The following sample imports [["value1"],["value2"]] to 'M2:M3' as shown in your script. As a sample, Sheet id is 0. The range is required to be inputted the GridRange.

var sheets = google.sheets('v4');
var request = {
  spreadsheetId: SPREADSHEET_ID,
  resource: {
    "requests": [
      {
        "updateCells": {
          "rows": [
            {
              "values": [
                {
                  "userEnteredValue": {
                    "stringValue": "value1"
                  },
                  "userEnteredFormat": {
                    "textFormat": {
                      "bold": true
                    }
                  }
                }
              ]
            },
            {
              "values": [
                {
                  "userEnteredValue": {
                    "stringValue": "value2"
                  },
                  "userEnteredFormat": {
                    "textFormat": {
                      "bold": true
                    }
                  }
                }
              ]
            }
          ],
          "range": {
            "sheetId": 0, // Sheet id
            "startColumnIndex": 12,
            "endColumnIndex": 13,
            "startRowIndex": 1,
            "endRowIndex": 3
          },
          "fields": "*"
        }
      }
    ]
  },
  auth: auth,
};
sheets.spreadsheets.batchUpdate(request, function(err, response) {
  if (err) {
    console.error(err);
    return;
  }
  console.log(JSON.stringify(response, null, 2));
});

如果我误解了你的问题,对不起.

If I misunderstand your question, I'm sorry.

这篇关于Google Sheets API:插入格式化的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆