代码之家  ›  专栏  ›  技术社区  ›  EagleEye

如何用相同的文本颜色和格式将谷歌工作表复制到谷歌文档?

  •  0
  • EagleEye  · 技术社区  · 3 年前

    我正在尝试使用谷歌应用程序脚本将表格从谷歌表单导入谷歌文档。到目前为止,我已经能够以部分格式将数据表导入文档。这个 issue 是字体样式,当表导入到谷歌文档时,不保留文本颜色。

    这是代码:

    function appendTable() {
      
      // Replace these values with your Sheet ID, Document ID, and Sheet Name
      
      let ssId = '<Spreadsheet Id>' // REPLACE
      let docId = '<Google doc Id>' // REPLACE
      let sheetName = '<Sheet Name>' // REPLACE
      
      // Sheet
      let range = SpreadsheetApp.openById(ssId).getSheetByName(sheetName).getDataRange()
      let values = range.getValues();
      let backgroundColors = range.getBackgrounds();
      let styles = range.getTextStyles();
      
      
     // Document
      let body = DocumentApp.openById(docId).getBody();
      let table = body.appendTable(values);
     
      for (let i=0; i<table.getNumRows(); i++) {
        for (let j=0; j<table.getRow(i).getNumCells(); j++) {
          
          let docStyles = {};
          
          docStyles[DocumentApp.Attribute.BACKGROUND_COLOR] = backgroundColors[i][j];
          docStyles[DocumentApp.Attribute.FONT_SIZE] = styles[i][j].getFontSize();
          docStyles[DocumentApp.Attribute.BOLD] = styles[i][j].isBold();
         // docStyles[DocumentApp.Attribute.FOREGROUND_COLOR] = colors[i][j];
          
          table.getRow(i).getCell(j).setAttributes(docStyles);
        }
      }
    } 
    

    当脚本运行时,它会从工作表中导入下表:

    Sheet Table

    但是Google文档中导入的表丢失了格式,看起来是这样的:

    Doc Table

    你能告诉我我这里缺少什么吗?为什么谷歌文档中表格的字体样式和文本颜色与工作表不同?这是

    link

    到样本页。非常感谢。

    2 回复  |  直到 3 年前
        1
  •  4
  •   Tanaike    3 年前

    当我看到你展示的图片时,我想它需要包括 DocumentApp.Attribute.FOREGROUND_COLOR 在里面 docStyles 。那么,下面的修改怎么样?

    发件人:

     let docStyles = {};
     
     docStyles[DocumentApp.Attribute.BACKGROUND_COLOR] = backgroundColors[i][j];
     docStyles[DocumentApp.Attribute.FONT_SIZE] = styles[i][j].getFontSize();
     docStyles[DocumentApp.Attribute.BOLD] = styles[i][j].isBold();
    // docStyles[DocumentApp.Attribute.FOREGROUND_COLOR] = colors[i][j];
     
     table.getRow(i).getCell(j).setAttributes(docStyles);
    

    收件人:

    let docStyles = {};
    docStyles[DocumentApp.Attribute.BACKGROUND_COLOR] = backgroundColors[i][j];
    docStyles[DocumentApp.Attribute.FONT_SIZE] = styles[i][j].getFontSize();
    docStyles[DocumentApp.Attribute.BOLD] = styles[i][j].isBold();
    docStyles[DocumentApp.Attribute.FOREGROUND_COLOR] = styles[i][j].getForegroundColorObject().asRgbColor().asHexString(); // Added
    table.getRow(i).getCell(j).setAttributes(docStyles);
    

    参考:

        2
  •  0
  •   Cooper    3 年前

    试试这个:

    function appendTable() {
      const ss = SpreadsheetApp.openById("ssid")
      const sh = ss.getSheetByName("Sheet0");
      const rg = sh.getRange(1, 1, sh.getLastRow(), sh.getLastColumn())
      const vs = rg.getValues();
      const bs = rg.getBackgrounds();
      const fws = rg.getFontWeights();
      const fss = rg.getFontSizes();
      const sts = rg.getTextStyles();
      const d = DocumentApp.getActiveDocument();
      const b = d.getBody();
      const tbl = b.appendTable(vs);
      vs.forEach((r,i) => {
        r.forEach((c,j) => {
          let sty = {};
          sty[DocumentApp.Attribute.BACKGROUND_COLOR] = bs[i][j];
          sty[DocumentApp.Attribute.FONT_SIZE] = fss[i][j];
          sty[DocumentApp.Attribute.BOLD] = (fws[i][j] == 'bold') ? true: false;
          sty[DocumentApp.Attribute.FOREGROUND_COLOR] = sts[i][j].getForegroundColorObject().asRgbColor().asHexString();
          tbl.getRow(i).getCell(j).setAttributes(sty);
        });
      });
    }
    

    第0张:

    enter image description here

    文件:

    enter image description here