Good morning all,
I created a google app script which allows me to duplicate google docs templates using data from a google sheets file. Everything works perfectly, however I have a little problem.
The script always creates the google docs templates from the first line of the google sheets document.
For example, if the script was launched and processed 100 lines of google sheets, on the second use, it will recreate the first 100 templates. However, I would like the script to create only the templates for the new lines added to the google sheets.
I do not know if my request is clear but here is the script below:
function createDocument () {
var headers = Sheets.Spreadsheets.Values.get ('1tkcP1lA8QKIuAnur7WnGrHiVI1hrXdzRZPwp3ruSjrI', 'A1: I1');
var tactics = Sheets.Spreadsheets.Values.get ('1tkcP1lA8QKIuAnur7WnGrHiVI1hrXdzRZPwp3ruSjrI', 'A2: I1000');
var templateId = '170TkGYOQCegZWe5ude1ODPewLeyE1HsxasONhsIDY4I';
for (var i = 0; i <tactics.values.length; i ++) {
Logger.log (tactics);
var fpn = tactics.values [i] [0];
var name = tactics.values [i] [1];
var cp = tactics.values [i] [2];
var tel = tactics.values [i] [3];
var email = tactics.values [i] [4];
var type = tactics.values [i] [5];
var prog = tactics.values [i] [6];
var date = tactics.values [i] [7];
var time = tactics.values [i] [8];
var documentId = DriveApp.getFileById (templateId) .makeCopy (). getId ();
DriveApp.getFileById (documentId) .setName ('N °' + fpn + '' + name);
var head = DocumentApp.openById (documentId) .getHeader ();
head.replaceText ('## FP ##', fpn);
var body = DocumentApp.openById (documentId) .getBody ();
body.replaceText ('## NAME ##', name);
body.replaceText ('## CP ##', cp);
body.replaceText ('## EMAIL ##', email);
body.replaceText ('## TEL ##', tel);
body.replaceText ('## TYPE ##', type);
body.replaceText ('## PROG ##', prog);
body.replaceText ('## DATE ##', date);
body.replaceText ('## TIME ##', time);
}
}
Hoping someone can help me.
Regards,
Matthew
function createDocument () {
var headers,tactics;
var endRowToRange,rangeForDate,startRowToRange;
startRowToRange = 2;//Where the start row begins for this run of the code
endRowToRange = 1000;//Hard code the end row
rangeForDate = 'A' + startRowToRange + ":I" + endRowToRange;//Build the A1 Notation for the data range
Logger.log('rangeForDate ' + rangeForDate);
headers = Sheets.Spreadsheets.Values.get ('file_ID', 'A1: I1');
tactics = Sheets.Spreadsheets.Values.get ('file_ID', rangeForDate);
var templateId = 'file_ID';
startRowToRange = 2;//Where the start row begins for this run of the code
function createDocument () {
var headers,incrementBy,scriptProps,tactics;
var endRowToRange,rangeForDate,startRowToRange;
scriptProps = PropertiesService.getScriptProperties();
incrementBy = 1000;
startRowToRange = scriptProps.getProperty('startRow');//Where the start row begins for this run of the code
endRowToRange = scriptProps.getProperty('endRow');;//Retrieve the end row
if (!startRowToRange) {
startRowToRange = 2;
endRowToRange = 1000;
}
rangeForDate = 'A' + startRowToRange.toString() + ":I" + endRowToRange.toString();//Build the A1 Notation for the data range
Logger.log('rangeForDate ' + rangeForDate);
//All the code in the middle here
scriptProps.setProperty('startRow',endRowToRange + 1);//Save new start value
scriptProps.setProperty('endRow',endRowToRange + incrementBy);//Save a new value
endRowToRange = scriptProps.getProperty('endRow');;//Hard code the end row
}
function createDocument () { var headers,incrementBy,scriptProps,tactics; var endRowToRange,rangeForDate,startRowToRange; scriptProps = PropertiesService.getScriptProperties(); incrementBy = 1000;
startRowToRange = scriptProps.getProperty('startRow'); // Where the start row begins for this run of the code endRowToRange = scriptProps.getProperty('endRow'); // Retrieve the end row
if (!startRowToRange) { startRowToRange = 2;
endRowToRange = 13; } rangeForDate = 'A' + startRowToRange.toString() + ":I" + endRowToRange.toString(); // Build the A1 Notation for the data range
Logger.log('rangeForDate' + rangeForDate); // Where we fill the Google Docs template headers = Sheets.Spreadsheets.Values.get('1tkcP1lA8QKIuAnur7WnGrHiVI1hrXdzRZPwp3ruSjrI','A1:I1'); tactics = Sheets.Spreadsheets.Values.get('1tkcP1lA8QKIuAnur7WnGrHiVI1hrXdzRZPwp3ruSjrI',rangeForDate);
var templateId = '1uOJ_r2nqfgS8tE-__-CvJP7nyObPLbmqpwQaN2iq8QA';
for (var i = 0; i <tactics.values.length; i ++) {
Logger.log (tactics);
var fpn = tactics.values[i][0];
var nom = tactics.values[i][1];
var cp = tactics.values[i][2]; var tel = tactics.values[i][3]; var email = tactics.values[i][4]; var type = tactics.values[i][5]; var prog = tactics.values[i][6]; var date = tactics.values[i][7]; var time = tactics.values[i][8];
var documentId = DriveApp.getFileById(templateId).makeCopy().getId();
DriveApp.getFileById(documentId).setName(fpn + ' ' + nom.toUpperCase());
var head = DocumentApp.openById(documentId).getHeader(); head.replaceText ('##FP##', fpn);
var body = DocumentApp.openById(documentId).getBody(); body.replaceText ('##NOM##', nom); body.replaceText ('##CP##', cp); body.replaceText ('##EMAIL##', email); body.replaceText ('##TEL##', tel); body.replaceText ('##TYPE##', type); body.replaceText ('##PROG##', prog); body.replaceText ('##DATE##', date); body.replaceText ('##TIME##', time);
} // Loop to retreive the values processed before scriptProps.setProperty('startRow',endRowToRange + 1); // Save new start value scriptProps.setProperty('endRow',endRowToRange + incrementBy); // Save a new value endRowToRange = scriptProps.getProperty('endRow'); // Hard code the end row }
tactics = Sheets.Spreadsheets.Values.get('1tkcP1lA8QKIuAnur7WnGrHiVI1hrXdzRZPwp3ruSjrI',rangeForDate);
function createDocument () {
var headers,i,incrementBy,L,scriptProps,tactics;
var endRowToRange,rangeForDate,startRowToRange;
scriptProps = PropertiesService.getScriptProperties();
incrementBy = 1000;
startRowToRange = scriptProps.getProperty('startRow'); // Where the start row begins for this run of the code
endRowToRange = Number(startRowToRange) + incrementBy;
endRowToRange = endRowToRange.toString();//Needs to be a string to concatenate the A1 notation
startRowToRange = startRowToRange.slice(0,startRowToRange.indexOf("."));//Remove the decimal places FROM THE STRING
endRowToRange = endRowToRange.slice(0,endRowToRange.indexOf("."));//Remove the decimal places
if (!startRowToRange) {
startRowToRange = 2;
endRowToRange = 13;
}
rangeForDate = 'A' + startRowToRange + ":I" + endRowToRange; // Build the A1 Notation for the data range
Logger.log('rangeForDate' + rangeForDate);
// Where we fill the Google Docs template
headers = Sheets.Spreadsheets.Values.get('1tkcP1lA8QKIuAnur7WnGrHiVI1hrXdzRZPwp3ruSjrI','A1:I1');
tactics = Sheets.Spreadsheets.Values.get('1tkcP1lA8QKIuAnur7WnGrHiVI1hrXdzRZPwp3ruSjrI',rangeForDate);
var templateId = '1uOJ_r2nqfgS8tE-__-CvJP7nyObPLbmqpwQaN2iq8QA';
L = tactics.values.length;
var i;
for (i = 0; i < L; i ++) {
function createDocument () {
var headers,i,L,scriptProps,tactics;
var endRowToRange,rangeForDate,sh,sheetTabName,ss,ssFileID,startRowToRange,templateId;
ssFileID = '1tkcP1lA8QKIuAnur7WnGrHiVI1hrXdzRZPwp3ruSjrI';
templateId = '1uOJ_r2nqfgS8tE-__-CvJP7nyObPLbmqpwQaN2iq8QA';
sheetTabName = "Enter sheet tab name here";
ss = SpreadsheetApp.openById(ssFileID);
sh = ss.getSheetByName(sheetTabName);
endRowToRange = sh.getLastRow();//The end row number will always be the last row in the sheet tab
scriptProps = PropertiesService.getScriptProperties();
startRowToRange = scriptProps.getProperty('startRow'); // Where the start row begins for this run of the code
endRowToRange = endRowToRange.toString();//Needs to be a string to concatenate the A1 notation
startRowToRange = startRowToRange.slice(0,startRowToRange.indexOf("."));//Remove the decimal places FROM THE STRING
endRowToRange = endRowToRange.slice(0,endRowToRange.indexOf("."));//Remove the decimal places
if (!startRowToRange) {
startRowToRange = 2;
}
rangeForDate = 'A' + startRowToRange + ":I" + endRowToRange; // Build the A1 Notation for the data range
Logger.log('rangeForDate' + rangeForDate);
// Where we fill the Google Docs template
headers = Sheets.Spreadsheets.Values.get(ssFileID,'A1:I1');
tactics = Sheets.Spreadsheets.Values.get(ssFileID,rangeForDate);
Hi,
scriptProps.setProperty('startRow',endRowToRange + 1); // Save new start value
endRowToRange = sh.getLastRow();//The end row number will always be the last row in the sheet tab
if (cellValue == "") {//Test for a blank value
//its an empty cell
}
var scriptProps = PropertiesService.getScriptProperties();
var startRowToRange = scriptProps.getProperty('startRow');//Get the stored row value
To Save (set) value
scriptProps.setProperty('startRow', a value goes here); // Save value to Property Service
'startRow' is the key name.
I want you to understand the code in case you need to modify it, or write some other code.
if (cellValue == "") {//Test for a blank value
//its an empty cell
}