Google apps script 将特定单元格复制到Google文档的Google应用程序脚本?
如何从Google工作表复制单元格?像不同的细胞 A32-谷歌文档中的标题 B33-主题 B34-主体1 B35-主体2 所以在谷歌文档中,它看起来像电子邮件内容 我尝试在代码中创建,但它似乎很接近,但我不知道如何添加新行、放置标题,并且不更改文本样式Google apps script 将特定单元格复制到Google文档的Google应用程序脚本?,google-apps-script,google-sheets,google-docs,Google Apps Script,Google Sheets,Google Docs,如何从Google工作表复制单元格?像不同的细胞 A32-谷歌文档中的标题 B33-主题 B34-主体1 B35-主体2 所以在谷歌文档中,它看起来像电子邮件内容 我尝试在代码中创建,但它似乎很接近,但我不知道如何添加新行、放置标题,并且不更改文本样式 function copyTest() { var ss = SpreadsheetApp.getActiveSheet(); // create a new document and add student as e
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);
}
谢谢你的帮助 发行
无法更改所创建文档中标题和主题的样式
解决方案
您只需添加附加文本的粗体和字体大小属性。您可以找到有关如何使用这些方法的更多信息,下面是使用注释说明功能的代码片段:
函数copyTest(){
var ss=SpreadsheetApp.getActiveSheet();
//创建新文档并将学生添加为编辑器
var newDoc=DocumentApp.create(“复制:”+SpreadsheetApp.getActiveSpreadsheet().getName());
var targetDoc=newDoc.getId();
var header=ss.getRange('A32').getValues();
//将粗体和较大的部分与其他部分分开,使其更加清晰。
var subc=“主题:”;
var subtitle=ss.getRange('B2').getValues();
var copy=ss.getRange('D34:D40').getValues();
var body=newDoc.getBody();
body.editAsText().appendText(标题);
body.editAsText().appendText(“\n\n”);
//将这些样式从字符0(对应于文本开头)添加到标题
//第17个字符是主题的最后一部分:。这将使这些字符介于两者之间
//粗体和该字体大小(25),其余将保持它们作为普通纯文本。
body.editAsText().appendText(subc).setBold(0,17,true).setFontSize(0,17,25);
body.editAsText().appendText(subtitle);
body.editAsText().appendText(“\n\n”);
body.editAsText().appendText(复制);
}
您能否提供电子表格的图像以及您希望文档外观的图像。然后您的描述可能会变得更有意义。您好,这会有帮助吗?您好,设置框首先应用于标题。此外,如何将文本更改为标题4?并按原样粘贴“副本”,即使其中包含文本中的粗体文本?ThanksI已使用更多资源更新了答案,以实现您的目标。请检查我附加到文档的链接,了解如何使用APIhi在google文档中添加不同样式的文本,谢谢。但对于副本,我遇到了此错误“异常:参数(编号[])不匹配DocumentApp.Body.Append段落“”的方法签名,这是因为copy是从B3和B4生成的一个值字符串。您需要将该数组转换为字符串,或者分别获取值并添加它们。请尝试以下操作:var fieldB3=ss.getRange('B3').getValue();var fieldB4=ss.getRange('B4').getValue()在<代码>附录(FieldB3+FieldB4)中使用;也可以考虑接受我的答案,如果它能让其他用户知道工作:D