Change the line height of all content in a Google doc using the API via nodejs using batchupdate()

夙愿已清 提交于 2021-01-29 19:32:29

问题


Using Method: documents.get I can retrieve a document. The result is structured as described at Resource: documents. However, I need to select the whole document and change the line-height to 1.5, but I am unable to find a way to do it.

    var updateObject = {
      documentId: documentId,
      resource: {
        requests: [{
          'updateTextStyle': {
            'range': {
              'startIndex': 1,
              'endIndex': 0
            },
            'textStyle': {
              'line-height': 1.5
            },
            'fields': 'line-height'
          }
        }],
      },
    };
    docs.documents.batchUpdate(updateObject)
      .then(function(res) { // Modified
        console.log(res);
      }, function(err) {
        console.error(err);
      });

回答1:


  • Get your document and calculate start and end indexes of all paragraphs within the body.

  • Issue updateParagraphStyle request for each of the above paragraphs with linespacing as 150 for 1.5 times the normal space.




回答2:


I managed to do this using Google Apps Script...

function setLineHeight() {
  var d = DocumentApp.getActiveDocument();
  var b = d.getBody()
  var pars = b.getParagraphs();
  // for each paragraph in the active document...
  pars.forEach(function(e) {
      // clean up paragraphs that only contain whitespace
      e.setLineSpacing(1.5);  
  })  
}


来源:https://stackoverflow.com/questions/60394199/change-the-line-height-of-all-content-in-a-google-doc-using-the-api-via-nodejs-u

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!