繁体   English   中英

将特定单元格复制到 Google 文档的 Google App 脚本?

[英]Google App Script to Copy Specific Cells to a Google Document?

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

A32 - Google Doc 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);
    }

谢谢您的帮助!

问题

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

解决方案

您只需要添加附加文本的粗体字体大小属性。 您可以在此处找到有关如何使用这些方法的更多信息, 用于粗体此处用于字体大小这是一段代码,其中包含解释功能的注释:

 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); }

实现此目的的另一种方法是使用类段落,利用段落标题来设置默认样式或创建自定义样式,例如所需的标题 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); }

我希望这对你有帮助。 如果您需要其他任何东西或者您不理解某些东西,请告诉我。 :)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM