I've followed this guide from the Google Support :
My Google Sheet looks like this. The image shows the results. Originally Mary was checked.
Now the script is bound to Google Sheet.
function createSlide() {
try {
let spread = SpreadsheetApp.getActiveSpreadsheet();
let sheet = spread.getSheetByName("Sheet1");
let values = sheet.getDataRange().getValues();
values.shift(); // remove header
let update = false;
values.forEach( row => {
// if checked row[4] is true or link is present
if( row[4] || ( row[5] !== "" ) ) {
// open the template
let presentation = SlidesApp.openById("1KjOmacjS_iht2xoBxpnleb28pDMncAu_Ma2NM--PWGY");
let slides = presentation.getSlides();
// create a new presentation named for the employee
presentation = SlidesApp.create(row[0]);
// copy the template slides to the new presentation
slides.forEach( (slide,index) => {
presentation.insertSlide(index,slide);
}
);
// there is only one slide
slides = presentation.getSlides()[0];
slides.replaceAllText("{name}",row[0]);
slides.replaceAllText("{employee no}",row[1]);
slides.replaceAllText("{work shift}",row[2]);
slides.replaceAllText("{date of hire}",Utilities.formatDate(row[3],"PST","dd/MM/yyyy"));
// update the values
row[4] = false;
row[5] = presentation.getUrl();
update = true;
}
}
);
// update the spreadsheet
if( update ) {
values = values.map( row => row.slice(4,6) );
sheet.getRange(2,5,values.length,2).setValues(values);
}
}
catch(err) {
console.log(err);
}
}
The new presentation look like this.
Reference
CodePudding user response:
I have made some changes but still have an issue.
Here is my code for the moment :
function createSlide() {
try {
let spread = SpreadsheetApp.getActiveSpreadsheet();
let sheet = spread.getSheetByName("Feuille 1");
let values = sheet.getDataRange().getValues();
values.shift(); // remove header
let update = false;
values.forEach( row => {
// if checked row[4] is true or link is present
if( ( row[4] == true ) || ( row[5] == "" ) ) {
// open the template
//SpreadsheetApp.getActiveSpreadsheet().toast("Génération du GSlides en cours...⌛");
let presentation = SlidesApp.openById("1RXbjWlTnFVMEo88Ub2RPwi0UrkA-z_b1UWEbv8twKLo");
let slides = presentation.getSlides();
presentation = SlidesApp.create("Alvo - BR -" new Date());
slides.forEach( (slide,index) => {
presentation.insertSlide(index,slide);
}
);
let newSlides = presentation.getSlides();
newSlides[1].remove();
// there is only one slide
slides = presentation.getSlides()[0];
slides.replaceAllText("{{Vendor}}",row[0]);
slides.replaceAllText("{{Description}}",row[1]);
slides.replaceAllText("{{Order#}}",row[2]);
slides.replaceAllText("{{Type}}",row[3]);
//slides.replaceAllText("{date of hire}",Utilities.formatDate(row[3],"PST","dd/MM/yyyy"));
// update the values
row[4] = false;
row[5] = presentation.getUrl();
update = true;
}
}
);
// update the spreadsheet
if( update ) {
values = values.map( row => row.slice(4,6) );
sheet.getRange(2,5,values.length,2).setValues(values);
} } catch(err) {
console.log(err); } }
function onEdit(e) { // check if the edited cell is in column 5 if (e.range.getColumn() == 5) {
// check if the value of the edited cell is true
if (e.value == true) {
// call the createSlide function
createSlide();
} } }
The fact is that when I'm adding some content on any other cell of the sheet, some slides are generated (dunno why!).
I only want slides to be generated when I manually check the checkbox.
Looking for your help on it, thanks!
(made an onEdit function as a trigger, triggered when there is a modification on the sheet. Should be here that the bug come from??)