Google 脚本:google 文档中一行文本中的多种样式
Google scripts : Multiple styles within a line of text in google docs
我对 Google 脚本和一般的 javascript 还很陌生。我正在清点我爷爷的商店,我的目标是使用来自 google sheet 的数据自动创建目录(在 google 文档中)。我目前所做的就是使用脚本获取 sheet 中的数据,并将该信息放入 google 文档中,并在其周围添加一些简单的文本。
例如,在我的 sheet 中,我有关于对象的信息(类别、价格、描述、ID 等),我想在 google 文档中像这样显示它:
ID : 1
类别: 时钟
价格:1000
描述:
...第一个机械钟,采用边缘擒纵机构和 floot 或摆轮计时...
我遇到的问题是我不知道如何将文本设为粗体,而是将 google sheet 中的数据设为普通文本。我想我的问题更多的是如何设置同一行不同文本的样式?
The result I want and the result I have
我正在使用 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);
}
}
非常感谢您阅读我的问题,并提前感谢您的任何答复。这是我的第一个 post 所以我希望我做得很好。
祝你有个愉快的一天!
在您的脚本中,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("");
// ---
}
注:
- 我认为在您的脚本中,
}
of var nb = sheet_values.length;}
可能会在保存脚本时发生错误。请注意这一点。
我对 Google 脚本和一般的 javascript 还很陌生。我正在清点我爷爷的商店,我的目标是使用来自 google sheet 的数据自动创建目录(在 google 文档中)。我目前所做的就是使用脚本获取 sheet 中的数据,并将该信息放入 google 文档中,并在其周围添加一些简单的文本。
例如,在我的 sheet 中,我有关于对象的信息(类别、价格、描述、ID 等),我想在 google 文档中像这样显示它:
ID : 1
类别: 时钟
价格:1000
描述:
...第一个机械钟,采用边缘擒纵机构和 floot 或摆轮计时...
我遇到的问题是我不知道如何将文本设为粗体,而是将 google sheet 中的数据设为普通文本。我想我的问题更多的是如何设置同一行不同文本的样式?
The result I want and the result I have
我正在使用 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);
}
}
非常感谢您阅读我的问题,并提前感谢您的任何答复。这是我的第一个 post 所以我希望我做得很好。
祝你有个愉快的一天!
在您的脚本中,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("");
// ---
}
注:
- 我认为在您的脚本中,
}
ofvar nb = sheet_values.length;}
可能会在保存脚本时发生错误。请注意这一点。