简体   繁体   中英

Google App Script to Copy Specific Cells to a Google Document?

How can I copy cells from a Google Sheet? Like Different cell

A32 - Header in Google Doc B33 - Subject B34 - Body 1 B35 - Body 2

So that in Google Document, it would look like an email content.

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 .

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. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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