Google Apps Script; Docs; convert selected element to HTML

前端 未结 2 1360
夕颜
夕颜 2021-01-15 04:40

I am just starting with Google Apps Script and following the Add-on quickstart

https://developers.google.com/apps-script/quickstart/docs

In the quickstart yo

2条回答
  •  囚心锁ツ
    2021-01-15 05:00

    Ended up making a script to support my use-case of bold+links+italics:

    function getHtmlOfElement(element) {
      var text = element.editAsText();
      var string = text.getText();
      var indices = text.getTextAttributeIndices();
      var output = [];
    
      for (var i = 0; i < indices.length; i++) {
        var offset = indices[i];
        var startPos = offset;
        var endPos = i+1 < indices.length ? indices[i+1]: string.length;
        var partText = string.substring(startPos, endPos);
    
        var isBold = text.isBold(offset);
        var isItalic = text.isItalic(offset);
        var linkUrl = text.getLinkUrl(offset);
    
        if (isBold) {
          output.push('');
        }
        if (isItalic) {
          output.push('');
        }
        if (linkUrl) {
          output.push('');
        }
    
        output.push(partText);
    
        if (isBold) {
          output.push('');
        }
        if (isItalic) {
          output.push('');
        }
        if (linkUrl) {
          output.push('');
        }
      }
    
      return output.join("");
    }
    

    You can simply call it using something like:

    getHtmlOfElement(myTableCell); // returns something like "Bold test."
    

提交回复
热议问题