【问题标题】:Google App Script to Copy Specific Cells to a Google Document?将特定单元格复制到 Google 文档的 Google App 脚本?
【发布时间】:2020-03-04 23:13:26
【问题描述】:

如何从 Google 表格中复制单元格?喜欢不同的细胞

A32 - Google 文档中的标题 B33 - 主题 B34 - 身体 1 B35 - 身体 2

因此在 Google 文档中,它看起来像电子邮件内容。

我尝试在代码中创建,但它似乎接近但我不知道如何添加新行并放置标题并且不更改文本样式。

 function copyTest() {
      var ss = SpreadsheetApp.getActiveSheet();

      // create a new document and add student as editor
      var newDoc = DocumentApp.create("Copy: " + SpreadsheetApp.getActiveSpreadsheet().getName());
      var targetDoc = newDoc.getId();

      var header = ss.getRange('A32').getValues();
      var subj = "Subject: " + ss.getRange('D33').getValues();
      var copy = ss.getRange('D34:D40').getValues();

      var body = newDoc.getBody();

      body.editAsText().appendText(header);
      body.editAsText().appendText("\n\n");
      body.editAsText().appendText(subj);
      body.editAsText().appendText("\n\n");
      body.editAsText().appendText(copy);
    }

感谢您的帮助!

【问题讨论】:

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


【解决方案1】:

问题

在创建的文档中无法更改页眉和主题的样式。

解决方案

您只需添加附加文本的 BoldFont Size 属性。您可以找到更多关于如何使用这些方法here for the boldhere for the font size 的信息,下面是使用 cmets 解释功能的一段代码:

 function copyTest() {
      var ss = SpreadsheetApp.getActiveSheet();

      // create a new document and add student as editor
      var newDoc = DocumentApp.create("Copy: " + SpreadsheetApp.getActiveSpreadsheet().getName());
      var targetDoc = newDoc.getId();

      var header = ss.getRange('A32').getValues();
      // separated the parts that go in bold and big from the other parts to make it more clear.
      var subj = "Subject: ";
      var subjtitle = ss.getRange('B2').getValues();
      var copy = ss.getRange('D34:D40').getValues();

      var body = newDoc.getBody();

      body.editAsText().appendText(header);
      body.editAsText().appendText("\n\n");
      // Add these styles for headigs from the character 0 (corresponding to the beginning of the text to the
      // character 17 which is the last part of the Subject: . This will make those characters in between
      // bold and with that font size (25) and the rest will keep them as normal plain text.
      body.editAsText().appendText(subj).setBold(0,17,true).setFontSize(0, 17, 25);
      body.editAsText().appendText(subjtitle);
      body.editAsText().appendText("\n\n");
      body.editAsText().appendText(copy);
    }

实现此目的的其他方法是使用class paragraph,利用paragraph headings 设置默认样式或创建自定义样式,例如您所需的标题4

以下代码只是实现的示例:

function copyTest() {
  var ss = SpreadsheetApp.getActiveSheet();
  
  // create a new document and add student as editor
  var newDoc = DocumentApp.create("Copy: " + SpreadsheetApp.getActiveSpreadsheet().getName());
  var targetDoc = newDoc.getId();
  
  var headertext = ss.getRange('A1').getValue();
  var subj = "Subject: ";
  var subjtitle = ss.getRange('B2').getValue();
  var copy = ss.getRange('B3:B4').getValues();
  
  var body =  newDoc.getBody();
  
  var header = body.appendParagraph(headertext+'\n\n'+subj);
  header.setHeading(DocumentApp.ParagraphHeading.HEADING4);
    
}

我希望这对您有所帮助。让我知道您是否需要其他任何内容,或者您​​是否不理解某些内容。 :)

【讨论】:

  • 您好,setBold 首先应用于标题。另外,如何将文本更改为标题4?并按原样粘贴“复制”,即使它在文本中包含粗体文本?谢谢
  • 我已经用更多资源更新了答案,以实现您的目标。请查看我附在文档中的链接,了解如何使用 API 在 google docs 中添加不同样式的文本
  • 嗨,谢谢你的作品。但是对于副本,我遇到此错误“异常:参数(编号 [])与 DocumentApp.Body.appendParagraph 的方法签名不匹配。”,
  • 那是因为 copy 是从 B3 和 B4 开始的一串值。您需要将该数组转换为字符串或单独获取值并添加它们。试试这个:var fieldB3 = ss.getRange('B3').getValue(); var fieldB4 = ss.getRange('B4').getValue();,然后在appendParagraph(fieldB3+fieldB4);中使用
  • 如果它可以让其他用户知道它确实有效,也可以考虑接受我的回答:D
猜你喜欢
  • 2020-06-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-22
  • 2020-04-29
  • 2018-01-21
  • 2013-07-02
  • 2012-06-01
相关资源
最近更新 更多