簡體   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