I am quite new to Google scripts and javascript in general. I am doing the inventory of my grandpa's shop and my goal is to automate the creation of a catalogue (in google docs) with data from a google sheets. All I have done at the moment is fetch the data in the sheet with a script and put that information in a google docs with some simple text around it.
For example in my sheet I have information about an object (category, price, description, id, etc) and I would to like to display it like so in a google docs :
ID : 1
Category : clocks
Price : 1000
Description :
...The first mechanical clocks, employing the verge escapement mechanism with a foliot or balance wheel timekeep...
The problem I have is that I dont know how to put the text in bold but the data from my google sheet in normal text. I guess my question is more how do I style text, that is on the same line, differently ?
The result I want and the result I have
I am using setAttribute but this puts the whole text in bold :
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);
}
}
Thanks a lot for reading my question and thanks in advance for any responses. This is my first post so I hope I did okay.
Have a good day !
In your script, text.setAttributes(style_objet)
is used to body.appendParagraph
. By this, the attribute is reflected to all texts. I think that the reason of your issue is due to this. In order to achieve your goal, in this case, for example, how about putting the values of ID :
and id
by separating and giving each attribute?
When your script is modified, how about the following modification?
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;}
might occur an error when the script is saved. Please be careful this.