【问题标题】:Google Docs table - how to format table (cell) and open document in browserGoogle Docs 表格 - 如何格式化表格(单元格)并在浏览器中打开文档
【发布时间】:2021-09-20 15:23:20
【问题描述】:

使用 Google 表格,我创建了一个计算表,我想使用 Google Apps 脚本将其复制到 Google 文档中的报价模板。为此,我使用在此问题中找到的解决方案:Using Google Apps Script to Copy from Google Sheets Data Table to Google Documents Table.

我使用(确切地说)第一个示例脚本,没有考虑列宽,因为这对我来说不是问题。
复制效果很好,但我仍然有一些问题:

  1. 所有单元格值都被复制为文本,因此在 Google Docs 表中我必须重新格式化一些欧元值
  2. 在 Google Docs 表格中,所有单元格都左对齐,我需要将列与欧元值右对齐
  3. 脚本使用“body.appendTable(values)”在文档末尾插入表格数据,但如何确定要在文档中的哪个位置放置表格?
  4. 从模板创建新文档后,我想直接在浏览器中打开它

我的脚本绑定到 Google Sheet 文档,我从自定义菜单启动脚本。
你可以看到calculation sheet heretemplate here.

希望能找到可以提供帮助的人。


编辑回答来自 cmets 的问题:

  1. 嘿,J.G.,也许问题解释得不够清楚,或者我不够聪明,无法处理您提出的解决方案:
    setNumberFormat() 仅在电子表格上为我工作,但我必须将表格格式化为Google Docs 文档。
    使用insertTable(childIndex, table),我只能在文档的开头或结尾插入表格。我错过了什么?

  2. 好的,在这种情况下,它将表格放在第 5 行,还尝试了许多其他索引,并且能够到达我想要的位置。但这对我来说不够可靠,因为如果模板发生变化(它会改变!)我必须再次找到正确的索引并更改脚本。或者是否有可能在文档中搜索关键字并通过脚本获取它的 childIndex?

【问题讨论】:

标签: google-apps-script google-sheets google-docs


【解决方案1】:

问题 1-3 很容易解决。这是工作脚本:

代码:

function myFunction() {
  // Get Google Sheet data
  var ssId = 'sample sheet id';
  var docId = 'sample doc id';
  var ss = SpreadsheetApp.openById(ssId);  // Please set the Spreadsheet ID.
  var sheet = ss.getSheetByName("DataFilter");
  var range = sheet.getRange(4, 1, 1, 5).getDataRegion(SpreadsheetApp.Dimension.ROWS);
  // to include euro signs
  var values = range.getDisplayValues();
  var backgroundColors = range.getBackgrounds();
  var styles = range.getTextStyles();

  // Position to paste data in Google Docs
  var body = DocumentApp.openById(docId).getBody();
  var numElements = body.getNumChildren();
  // if 'Table_goes_here' is not found, append to bottom
  var index = numElements;

  // var index = body.getChildIndex(body.findText('Table_goes_here').getElement().getParent());
  // index wasn't found using the code above for some reason, so doing loop instead
  for (var i = 0; i < numElements; i++) {
    var child = body.getChild(i);
    if (child.asText().getText() == 'Table_goes_here') {
      index = i;
      // remove child
      body.removeChild(child);
      break;
    }
  }
  // then insert your table
  var table = body.insertTable(index, values);
  table.setBorderWidth(0);
  for (var i = 0; i < table.getNumRows(); i++) {
    for (var j = 0; j < table.getRow(i).getNumCells(); j++) {
      var obj = {};
      obj[DocumentApp.Attribute.BACKGROUND_COLOR] = backgroundColors[i][j];
      obj[DocumentApp.Attribute.FONT_SIZE] = styles[i][j].getFontSize();
      if (styles[i][j].isBold()) {
        obj[DocumentApp.Attribute.BOLD] = true;
      }
      // if euro sign is found in text, align right
      if (table.getRow(i).getCell(j).getText().includes('€')) {
        table.getRow(i).getCell(j).getChild(0).asParagraph().setAlignment(DocumentApp.HorizontalAlignment.RIGHT);
      }
      table.getRow(i).getCell(j).setAttributes(obj);
    }
  }
}

表格开始:

表格结束:

注意:

  • 问题 4 是一个棘手的问题。 Apps 脚本无法轻松与浏览器 (afaik) 通信,因为它运行在服务器端,而不是客户端。您可以尝试在 WebApp 上显示该文档。

资源:

【讨论】:

  • 嗨,NaziA,只有知道如何解决才容易!你的剧本非常适合我(赞成!)我今天学到了一些新东西。对于要在浏览器中打开的文档,我稍后会看到。非常感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-11-21
  • 1970-01-01
  • 1970-01-01
  • 2019-11-26
  • 2022-10-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多