简体   繁体   English

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

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

How can I copy cells from a Google Sheet?如何从 Google Sheet 复制单元格? Like Different cell喜欢不同的细胞

A32 - Header in Google Doc B33 - Subject B34 - Body 1 B35 - Body 2 A32 - Google Doc B33 中的标题 - 主题 B34 - 正文 1 B35 - 正文 2

So that in Google Document, it would look like an email content.因此,在 Google 文档中,它看起来像电子邮件内容。

I tried creating in code but it seems its near but I don't know how to add a new line and put headings and don't change the text styles.我尝试在代码中创建,但它似乎接近但我不知道如何添加新行并放置标题并且不更改文本样式。

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

Thanks for the help!谢谢您的帮助!

Issue问题

Cannot change the styles of the header and subject in the created document.无法更改创建的文档中标题和主题的样式。

Solution解决方案

You just need to add the Bold and Font Size properties of your text appended.您只需要添加附加文本的粗体字体大小属性。 YOu can find more information about how to work with these methods here for the bold and here for the font size Here is the piece of code for doing so with comments explaining the functionality:您可以在此处找到有关如何使用这些方法的更多信息, 用于粗体此处用于字体大小这是一段代码,其中包含解释功能的注释:

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

Other way of achieving this is to use the class paragraph taking advantage of the paragraph headings to set the default styles or to create custom styles such as your required heading 4 .实现此目的的另一种方法是使用类段落,利用段落标题来设置默认样式或创建自定义样式,例如所需的标题 4

The following piece of code is just an example of the implementaion of this:下面的一段代码只是这个实现的一个例子:

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

I hope this has helped you.我希望这对你有帮助。 Let me know if you need anything else or if you did not understood something.如果您需要其他任何东西或者您不理解某些东西,请告诉我。 :) :)

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

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