首页 > 解决方案 > 谷歌脚本:谷歌文档中一行文本中的多种样式

问题描述

一般来说,我对 Google 脚本和 javascript 很陌生。我正在清点我爷爷的商店,我的目标是使用来自谷歌表格的数据自动创建目录(在谷歌文档中)。目前我所做的就是使用脚本获取工作表中的数据,并将该信息放入谷歌文档中,并在其周围添加一些简单的文本。

例如,在我的工作表中,我有关于一个对象的信息(类别、价格、描述、id 等),我想在 google docs 中像这样显示它:

ID : 1
类别 :钟表
价格 : 1000
描述 : ...第一个机械时钟,采用边缘擒纵机构和对折或摆轮计时...

我遇到的问题是我不知道如何将文本以粗体显示,但我的谷歌表中的数据以普通文本显示。我想我的问题更多是如何设置文本样式,即在同一行,不同?

我想要的结果和我得到的结果

我正在使用 setAttribute 但这会将整个文本以粗体显示:

function mailMerging(){
var doc = DocumentApp.create('Surprise document');
var body = doc.getBody();

var sheet = SpreadsheetApp.openById('1V_3KwMQPNYagjJdfbas7VjyjodUt')
var sheet_values = sheet.getDataRange().getValues()
var nb = sheet_values.length;}

// Style pour décrire chaque objet
var style_objet = {};
style_objet[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT] =
  DocumentApp.HorizontalAlignment.LEFT;
style_objet[DocumentApp.Attribute.FONT_FAMILY] = 'Calibri';
style_objet[DocumentApp.Attribute.FONT_SIZE] = 12;
style_objet[DocumentApp.Attribute.BOLD] = true;

// On va chercher les infos pour chaque objet 1 à 1
for (var c=1; c<nb; c++){

var categorie = sheet_values[c][0];
var marque = sheet_values[c][1];
var prix = sheet_values[c][2];
var description = sheet_values[c][3];
var id = sheet_values[c][4];

var text = body.appendParagraph("ID : "+ id +"\nCatégorie : "+ categorie +"\nMarque : "+ marque +"\nPrix : "+ prix +"\nDescription : \n"+ description+"\n \n");
text.setAttributes(style_objet);
}

}

非常感谢您阅读我的问题,并提前感谢您的任何回复。这是我的第一篇文章,所以我希望我做得很好。

祝你有美好的一天 !

标签: google-apps-scriptgoogle-sheetsformattinggoogle-docs

解决方案


在您的脚本中,text.setAttributes(style_objet)用于body.appendParagraph. 这样,属性就会反映到所有文本中。我认为你的问题的原因是由于这个。为了实现你的目标,在这种情况下,例如,如何通过分离和赋予每个属性来放置ID : 和的值?id

当你的脚本被修改时,下面的修改呢?

从:

var style_objet = {};
style_objet[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT] = DocumentApp.HorizontalAlignment.LEFT;
style_objet[DocumentApp.Attribute.FONT_FAMILY] = 'Calibri';
style_objet[DocumentApp.Attribute.FONT_SIZE] = 12;
style_objet[DocumentApp.Attribute.BOLD] = true;

// On va chercher les infos pour chaque objet 1 à 1
for (var c=1; c<nb; c++){

var categorie = sheet_values[c][0];
var marque = sheet_values[c][1];
var prix = sheet_values[c][2];
var description = sheet_values[c][3];
var id = sheet_values[c][4];

var text = body.appendParagraph("ID : "+ id +"\nCatégorie : "+ categorie +"\nMarque : "+ marque +"\nPrix : "+ prix +"\nDescription : \n"+ description+"\n \n");
text.setAttributes(style_objet);
}

至:

var style_objet = {};
style_objet[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT] = DocumentApp.HorizontalAlignment.LEFT;
style_objet[DocumentApp.Attribute.FONT_FAMILY] = 'Calibri';
style_objet[DocumentApp.Attribute.FONT_SIZE] = 12;
style_objet[DocumentApp.Attribute.BOLD] = true;

// --- I added below script.
var style_objet2 = {};
style_objet2[DocumentApp.Attribute.BOLD] = false;
// ---

// On va chercher les infos pour chaque objet 1 à 1
for (var c=1; c<nb; c++){
  var categorie = sheet_values[c][0];
  var marque = sheet_values[c][1];
  var prix = sheet_values[c][2];
  var description = sheet_values[c][3];
  var id = sheet_values[c][4];

  // --- I modified below script.
  [
    {title: "ID : ", value: id},
    {title: "Catégorie : ", value: categorie},
    {title: "Marque : ", value: marque},
    {title: "Prix : ", value: prix},
    {title: "Description : ", value: description}
  ].forEach(({title, value}) =>
    body.appendParagraph(title).setAttributes(style_objet).appendText(value).setAttributes(style_objet2)
  );
  body.appendParagraph("");
  // ---
}

笔记:

  • 我认为在您的脚本中,保存脚本}var nb = sheet_values.length;}可能会发生错误。请注意这一点。

推荐阅读