Cellule du dessus
Bon j'ai réussi en faisait ça au final , ca ne doit pas être très propre mais ca fonctionne
// Populate the template with our form responses and save the file.
populateTemplate(document, response_data);
document.saveAndClose()
var doc = DocumentApp.openById(document_copy.getId());
var docblob = document_copy.getAs('application/pdf');
docblob.setName(document_copy.getName() + ".pdf");
var file = DriveApp.createFile(docblob);
console.log('Your PDF file is available at ' + file.getUrl());
}Par contre quel ligne je pourrai ajouter pour choisir un répertoire de destination du pdf ?
Actuellement il part à la racine du drive
Désolé, j'étais absent !
Bon tu t'es bien débrouillée quand même. Mais n'ayant plus accès à ta feuille, j'ai du mal à voir quelles sont les différents documents générés.
Bon j'ai réussi a généré le pdf en changeant le code sauf que c'est mon template qui est sur le pdf --'
Il faut bien sûr mettre ceci sur le document générés (customisé)
convertPDF(ici_mon_document_généré.getId());Désolé je n'était pas la ce soir , voici le lien : https://docs.google.com/spreadsheets/d/1n-dyVRFN48l_X_PJrKD7nnPVw4QGLwdTECLZa8xbPGk/edit?usp=sharing
ainsi que mon code que j'ai réussi à bidouiller :
// @ts-nocheck
///////////////////////////////////////////////////////////////////////////////////////////////
// BEGIN EDITS ////////////////////////////////////////////////////////////////////////////////
const TEMPLATE_FILE_ID = '1c0AUjcwsjs7-A3IyEwEk6dBnktlv5zqn_IsnxJAyxmc';
const DESTINATION_FOLDER_ID = '1fZTsW5t5JPVzxwR8jQ0zb6huWdlfMXi-';
const CURRENCY_SIGN = '€';
// END EDITS //////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////
// WARNING: EDITING ANYTHING BELOW THIS LINE WILL CHANGE THE BEHAVIOR OF THE SCRIPT. //////////
// DO SO AT YOUR OWN RISK.//// ////////////////////////////////////////////////////////////////
// ----------------------------------------------------------------------------------------- //
// Converts a float to a string value in the desired currency format
function toCurrency(num) {
var fmt = Number(num).toFixed(2);
return `${fmt}${CURRENCY_SIGN}`;
}
// Format datetimes to: YYYY-MM-DD
function toDateFmt(dt_string) {
var millis = Date.parse(dt_string);
var date = new Date(millis);
var year = date.getFullYear();
var month = ("0" + (date.getMonth() + 1)).slice(-2);
var day = ("0" + date.getDate()).slice(-2);
// Return the date in YYYY-mm-dd format
return `${day}/${month}/${year}`;
}
// Parse and extract the data submitted through the form.
function parseFormData(values, header) {
// Set temporary variables to hold prices and data.
var subtotal = 0;
var discount = 0;
var response_data = {};
// Iterate through all of our response data and add the keys (headers)
// and values (data) to the response dictionary object.
for (var i = 0; i < values.length; i++) {
// Extract the key and value
var key = header[i];
var value = values[i];
// If we have a price, add it to the running subtotal and format it to the
// desired currency.
if (key.toLowerCase().includes("xt")) {
subtotal += value;
value = toCurrency(value);
// If there is a discount, track it so we can adjust the total later and
// format it to the desired currency.
} else if (key.toLowerCase().includes("réduction")) {
discount += value;
value = toCurrency(value);
// Format dates
} else if (key.toLowerCase().includes("date")) {
value = toDateFmt(value);
}
// Add the key/value data pair to the response dictionary.
response_data[key] = value;
}
// Once all data is added, we'll adjust the subtotal and total
response_data["sub_total"] = toCurrency(subtotal);
response_data["total"] = toCurrency(subtotal - discount);
return response_data;
}
// Helper function to inject data into the template
function populateTemplate(document, response_data) {
// Get the document header and body (which contains the text we'll be replacing).
var document_header = document.getHeader();
var document_body = document.getBody();
// Replace variables in the header
for (var key in response_data) {
var match_text = `{{${key}}}`;
var value = response_data[key];
// Replace our template with the final values
document_header.replaceText(match_text, value);
document_body.replaceText(match_text, value);
}
}
// Function to populate the template form
function createDocFromForm() {
// Get active sheet and tab of our response data spreadsheet.
var sheet = SpreadsheetApp.getActiveSheet();
var last_row = sheet.getLastRow() - 1;
// Get the data from the spreadsheet.
var range = sheet.getDataRange();
// Identify the most recent entry and save the data in a variable.
var data = range.getValues()[last_row];
// Extract the headers of the response data to automate string replacement in our template.
var headers = range.getValues()[0];
// Parse the form data.
var response_data = parseFormData(data, headers);
// Retreive the template file and destination folder.
var template_file = DriveApp.getFileById(TEMPLATE_FILE_ID);
var target_folder = DriveApp.getFolderById(DESTINATION_FOLDER_ID);
// Copy the template file so we can populate it with our data.
// The name of the file will be the company name and the invoice number in the format: DATE_COMPANY_NUMBER
var filename = `${response_data["Nom Prénom"]}_${response_data["Facture n°"]}`;
var document_copy = template_file.makeCopy(filename, target_folder);
// Open the copy.
var document = DocumentApp.openById(document_copy.getId());
// Populate the template with our form responses and save the file.
populateTemplate(document, response_data);
document.saveAndClose()
var doc = DocumentApp.openById(document_copy.getId());
var docblob = document_copy.getAs('application/pdf');
docblob.setName(document_copy.getName() + ".pdf");
var file = DriveApp.createFile(docblob);
console.log('Your PDF file is available at ' + file.getUrl());
}Par contre qu'est ce que je dois ajouter pour sélectionné un répertoire où enregistrer ce pdf ?
Pour le mettre dans un dossier particulier, mets l'ID du dossier ici
function convertPDF(docID) {
var folderID = '############'
var doc = DocumentApp.openById(docID);
var docblob = doc.getAs('application/pdf');
docblob.setName(doc.getName() + ".pdf");
var folder = DriveApp.getFolderById(folderID)
var file = folder.createFile(docblob);
console.log('Your PDF file is available at ' + file.getUrl());
}j'ai repris ma fonction générique, je te laisse intégrer à ton script.