Google 将特定单元格复制到 Google 文档的应用程序脚本?
Google App Script to Copy Specific Cells to a Google Document?
如何从 Google Sheet 复制单元格?喜欢不同的单元格
A32 - Google 文档中的 Header
B33 - 主题
B34 - Body 1
B35 - Body 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);
}
感谢您的帮助!
问题
Cannot change the styles of the header and subject in the created document.
解决方案
您只需添加附加文本的 粗体 和 字体大小 属性。您可以找到有关如何使用这些方法的更多信息 here for the bold and here for the font size 以下是执行此操作的代码段,并附有解释功能的注释:
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 taking advantage of the paragraph headings 设置默认样式或创建自定义样式,例如您需要的 heading 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);
}
希望对您有所帮助。让我知道您是否需要其他任何东西或者您不明白什么。 :)
如何从 Google Sheet 复制单元格?喜欢不同的单元格
A32 - Google 文档中的 Header B33 - 主题 B34 - Body 1 B35 - Body 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);
}
感谢您的帮助!
问题
Cannot change the styles of the header and subject in the created document.
解决方案
您只需添加附加文本的 粗体 和 字体大小 属性。您可以找到有关如何使用这些方法的更多信息 here for the bold and here for the font size 以下是执行此操作的代码段,并附有解释功能的注释:
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 taking advantage of the paragraph headings 设置默认样式或创建自定义样式,例如您需要的 heading 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);
}
希望对您有所帮助。让我知道您是否需要其他任何东西或者您不明白什么。 :)