Google appscipt - Exporting multiple PDF with a for loop, using a sheet as a template, unexpected result - for-loop

I'm trying to automate the pdf export of a sheet ('BL') which is filled depending on a cell value grabbed on a variable list on sheet 'EiBLdata' by 'i' on each loop.
It seems to work... more or less.
Instead of having 1st pdf with 1st value,
2nd pdf with 2nd value,
3rd pdf with 3rd value etc.
I get 1st pdf with 1st value,
2nd pdf with 1st value,
3rd pdf with 2nd value etc.
In the end only the 1st pdf has the right name, there is a shift in all the others and the last value isn't exported.
I'm quite a newbie with JavaScript and I admit there is a lot of copy/paste in my code, adapted to my purpose. I can't find what I'm doing wrong.
function printSelectedRange() {
var nomfeuille = "EiBLData"
var nomBL = "BL"
var cc = SpreadsheetApp.getActiveSpreadsheet();
var feuille = cc.getSheetByName(nomfeuille);
var BL = cc.getSheetByName(nomBL);
var tr = BL.getRange('B2').getValue();
var plage = feuille.getRange('A1:A15').getValues();
var cell0 = feuille.getRange(1,1).getValue();
BL.getRange('B2').setValue(cell0);
for (var i = 1; i <= 16; i++) {
var cell = feuille.getRange(i,1).getValue();
if (cell > 0) {
BL.getRange('B2').setValue(cell)
var sheetName = "BL";
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName(sheetName);
var ssUrl = ss.getUrl();
var sheetId= sheet.getSheetId();
var url = ssUrl.replace(/\/edit.*$/,'')
+ '/export?exportformat=pdf&format=pdf'
+ '&size=A6'
+ '&portrait=false'
+ '&fitw=false'
+ '&scale=4'
+ '&top_margin=0.35'
+ '&bottom_margin=0.00'
+ '&left_margin=0.35'
+ '&right_margin=0.0'
+ '&sheetnames=false'
+ '&printtitle=false'
+ '&pagenum=false'
+ '&gridlines=false'
+ '&fzr=FALSE'
+ '&gid='+sheetId;
var token = ScriptApp.getOAuthToken();
var docurl = UrlFetchApp.fetch(url, { headers: { 'Authorization': 'Bearer ' + token } });
var pdf = docurl.getAs('application/pdf');
var file = DriveApp.createFile(pdf);
var docId = sheet.getRange('F13').getValue();
var clientName = sheet.getRange('E9').getValue();
var docDate = sheet.getRange('F14').getValue();
var mois = docDate.getMonth()
docDate.setMonth((mois+1) % 12);
var docDateMMYY = docDate.getMonth()+"-"+docDate.getFullYear().toString().substr(-2);
var docName = "BL-"+docId+"-"+clientName+"-"+docDateMMYY ;
var folder = DriveApp.getFolderById("xxxxxxxxxxxxxxxxxxxx");
var finalFile = file.makeCopy(docName,folder);
file.setTrashed(true);
};
};
var cmdes = ss.getSheetByName('cmdes');
var raz = cmdes.getRange('S2:S501').setValue(false);
}

I think I had a good intuition and finally found the reason of my issue:
how-to-pause-app-scripts-until-spreadsheet-finishes-calculation
It looks like the loop runs faster than the spreadsheet calculation.
Sorry for inconvenience...

Related

Assistance with loop inside a loop for a loan deduction schedule google apps

Our company offers soft loans to employees. I am trying to write some code that will set up a loan deduction schedule once a loan is approved. This is all done on google sheets. The schedule then can be linked to payroll etc.
The approved loans will appear in a format like this:-
Loans Approved - [Serial, Employee ID,Amount, Monthly Deductions,Requested Date,Deduction Start Date]
I am looking to build an array that will have the first 4 elements that repeat and the deduction month to increase by 1
So far this is my code
function myFunction() {
var ss = SpreadsheetApp.getActive();
var sheet = ss.getSheetByName("Loans");
var range = sheet.getDataRange();
var data = range.getValues()
var lastRow = range.getLastRow()
var scheduleSheet = ss.getSheetByName("Schedule")
var scheuduleLastRow = scheduleSheet.getDataRange().getLastRow;
for(let i=1;i<lastRow;i++){
var serial = data [i][0]
var id = data [i][1]
var amount = data[i][2]
var monthlyRepayment = data [i][3]
var startDate = new Date (data [i][5])
var markScheduleDone = sheet.getRange(i+1,7)
var fullMonths = Math.floor(amount/monthlyRepayment)
var remainderMonth = (amount/monthlyRepayment)-fullMonths
var remainderAmount = Math.round(remainderMonth*monthlyRepayment)
for (let j=1;j<=fullMonths+1;j++){
var incrementalMonths = new Date(startDate.setMonth(startDate.getMonth()+1)) ;
}
var newArray = [serial,id,monthlyRepayment];
var remainderArray = [serial,id,remainderAmount];
var reptArray = Array(fullMonths).fill(newArray);
var finalArray = [...reptArray,remainderArray]
Logger.log(finalArray)
var toPasteto = scheduleSheet.getRange(scheuduleLastRow+1,1,finalArray.length,3)
toPasteto.setValues(finalArray)
markScheduleDone.setValue ("Done")
}
}
I am close but I cant figure out how to join the incrementalMonths to the finalarray.
This is the first time im using a loop within a loop
Also any guidance if I could have done this better?
Kinldy requesting some guidance
I'm not sure if this is exactly what you are looking for but try this.
Notice I fill the array finalArray with all the newArrays so I only have to setValues() once. Same with markDone.
I increment the month but if the day happens to fall outside the number of days in a month it will increment to another day. So for any 28 or 30 day months there should be another check but I didn't do that.
My particular style of coding is to always use a try {} catch() {} block, always terminate a line with semicolon ;, and to use let instead of var whenever possible.
function myFunction() {
try {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Loans");
var range = sheet.getDataRange();
var data = range.getValues()
var lastRow = range.getLastRow()
var scheduleSheet = ss.getSheetByName("Schedule")
var scheuduleLastRow = scheduleSheet.getDataRange().getLastRow();
let finalArray = [];
let markDone = [];
for(let i=1;i<lastRow;i++){
var serial = data [i][0];
var id = data [i][1];
var amount = data[i][2];
var monthlyRepayment = data [i][3];
var startDate = new Date (data [i][5]);
var fullMonths = Math.floor(amount/monthlyRepayment);
var remainderMonth = (amount/monthlyRepayment)-fullMonths;
var remainderAmount = Math.round(remainderMonth*monthlyRepayment);
let day = startDate.getDate();
if( day > 28 ) throw "This function has not adjusted for short months"
let month = startDate.getMonth();
let year = startDate.getFullYear();
let newArray = [];
for (let j=1;j<=fullMonths+1;j++){
month++;
if( month > 11 ) {
month = 0;
year++;
}
var date = new Date(year,month,day);
newArray.push([serial,id,monthlyRepayment,date])
}
newArray.push([serial,id,remainderAmount,date]);
finalArray = finalArray.concat(newArray);
Logger.log(newArray);
markDone.push(["Done"]);
}
sheet.getRange(2,7,markDone.length,1).setValues(markDone);
scheduleSheet.getRange(scheuduleLastRow+1,1,finalArray.length,4).setValues(finalArray);
}
catch(err) {
Logger.log(err)
}
}

Google Script - Macro to get data from a TABLE A and paste required values to Table B in sequence using LOOP

I seek help in finalizing a MACRO script for following task.
Basic requirements is to get data from 'Ref' tab (around 200 symbols) in a sequence to 'Mcontrol'!E3 (this tab imports a table from the Website)
From this table, sorted highest 2 values of Symbol/s is extracted and exported to 'OI EOD' tab,
i.e. required value is shown in 'OI EOD'! G5:K5.
Then, copy paste range 'OI EOD'! G5:K5 , to the matched row (symbol match to 'Mcontrol'!E3) in the table below ('OI EOD'!C6:C210).
I seek help in putting forEach condition in the macro. SO that next cycle of copy paste function is completed, for each new value in Cell_1 reference.
Hope ppl take notice of the help request from a novice and give good solution and guidance.
Sheet link is Following
https://docs.google.com/spreadsheets/d/1az2kas91KFxHcWhtWDg-g20hNYR-_zEiWPvYs-x42YU/edit?usp=sharing
Macro -
var wb = SpreadsheetApp.getActive();
var sh1= wb.getSheetByName('Mcontrol');
var sh2= wb.getSheetByName('OI EOD');
var R1 = 3
var C1 = 3
var R2 = 3
var C2 = 2
var R3 = 3
var C3 = 5
var cell_1 = sh1.getRange(R1,C1).getValue();
var cell_2 = sh1.getRange(R2,C2).getValue();
var cell_3 = sh1.getRange(R3,C3).getValue();
for(i=1;i<cell_2;i++);
cell_1 = i + 1;
sh1.getRange(R1,C1).setValue(cell_1);{
var Range_1 = sh2.getRange(6, 3, 300);
var row_i = 5 ; // source row no. for data copy paste
const Row_1 = row_i;
const Col_2 = 7;
const Row_Offet1 = 1;
const Col_Offet1 = 5;
var data = Range_1.getValues();
let ABC = cell_3;
let row = 1 + data.findIndex(users => {return users[0] == ABC});
var row_target = row + row_i ;
const sourceRange = sh2.getRange(Row_1,Col_2,Row_Offet1,Col_Offet1);
var destRange = sh2.getRange(row_target,Col_2,Row_Offet1,Col_Offet1);
sourceRange.copyTo(destRange,SpreadsheetApp.CopyPasteType.PASTE_VALUES,false);
for(k=1;k<cell_1.i;k++);
Logger.log(row_target);
Logger.log(k) ;
}
//NEXT
// cell_1=sh1.getRange(R1, C1);
// sh1.getRange(R1,C1).setValue(i);
}
}```
Thanks
Robin
Not sure if understand the task as a whole, so here is a guess.
This way you can get values from some start to some end 'symbols' (or numbers) and put these values in correct rows on the 'OI EOD' sheet:
function main() {
var start = 1;
var end = 5;
var ss = SpreadsheetApp.getActiveSpreadsheet();
var mcontrol_sheet = ss.getSheetByName('Mcontrol');
var oi_eod_sheet = ss.getSheetByName('OI EOD');
// get a list of all Symbols from column 'C' of the sheet 'OI EOD'
var symbols = oi_eod_sheet.getRange('c6:c').getValues().flat();
for (let i = start; i <= end; i++) {
// set the number i on the sheet 'Mcontrol' into the cell 'C3'
mcontrol_sheet.getRange('c3').setValue(i);
// wait to complete all the changes on the spreadsheet
SpreadsheetApp.flush();
// get the Symbol and Values from the range 'C5:K5' of the sheet 'OI EOD'
var [symbl, _, _, _, ...values] = oi_eod_sheet.getRange('c5:k5').getValues()[0];
// get the row index for this Symbol on the sheet 'Mcontrol'
var row = symbols.indexOf(symbl) + 6;
console.log({row}, {symbl}, {values});
// set the values on the sheet 'OI EOD' on the row with given index
oi_eod_sheet.getRange('g' + row + ':k' + row).setValues([values]);
}
}
It's up to you to decide how the start and end values could be defined. It can be values from two cells. Or it can be selected range. Or something else.

Add a status to each proccesed row in for loop that has an if statement with Google App Script

I've got a for loop in App script that is looking only at rows that have data in two columns. I'd like to set a status on each row that is actually processed, but the statuses get added to the wrong rows. When I add to i it adds to the whole length of the array, so I guess I shouldn't be trying to process each row, what am I doing wrong?
function auditReport() {
var sheetname = "Sheet1"; // name of data sheet ex. Form Responses 1
var colstoworkon = 10; // how many cols are filled with data f.e. by a form
var ss = SpreadsheetApp.getActiveSpreadsheet();
ss.setActiveSheet(ss.getSheetByName(sheetname));
var sheet = ss.getSheetByName(sheetname);
var data = sheet.getRange(3,1,sheet.getLastRow()-1,colstoworkon).getValues(); // starting with row 2 and column 1 as our upper-left most column,
//This makes it loops continuously and checks all not done rows
for (var i in data) {
if(data[i][1] && data[i][2]){//if email or copy are undefined just skip
var setStatus = sheet.getRange(i,4).setValue("done")
} // end of if
} // End of Loop
} //End of email function
Modification points:
In your script, from getRange(3,1,sheet.getLastRow()-1,colstoworkon), in this case, it is required to be getRange(3,1,sheet.getLastRow()-2,colstoworkon).
In the case of for (var i in data) {, i is the string type.
When you want to use sheet.getRange(i,4).setValue("done"), it is required to be sheet.getRange(Number(i) + 3, 4).setValue("done").
I thought that this might be the reason of your issue of but the statuses get added to the wrong rows..
In the case of if (data[i][1] && data[i][2]) {, if the value is 0, data[i][1] && data[i][2] is false.
When these points are reflected to your script, it becomes as follows.
Modified script:
function auditReport() {
var sheetname = "Sheet1";
var colstoworkon = 10;
var ss = SpreadsheetApp.getActiveSpreadsheet();
ss.setActiveSheet(ss.getSheetByName(sheetname));
var sheet = ss.getSheetByName(sheetname);
var data = sheet.getRange(3, 1, sheet.getLastRow() - 2, colstoworkon).getDisplayValues();
for (var i in data) {
if (data[i][1] && data[i][2]) {
var setStatus = sheet.getRange(Number(i) + 3, 4).setValue("done");
}
}
}
Or, your script can be also modified as follows. In this modification, done is put using the range list. By this, the process cost can be reduced a little.
function auditReport() {
var sheetname = "Sheet1";
var colstoworkon = 10;
var ss = SpreadsheetApp.getActiveSpreadsheet();
ss.setActiveSheet(ss.getSheetByName(sheetname));
var sheet = ss.getSheetByName(sheetname);
var data = sheet.getRange(3, 1, sheet.getLastRow() - 2, colstoworkon).getDisplayValues();
var ranges = data.map(([,b,c], i) => b && c ? `D${i + 3}` : "").filter(String);
if (ranges.length == 0) return;
sheet.getRangeList(ranges).setValue("done");
}
References:
for...in
getRangeList(a1Notations)

Why is this getting the same row twice in second loop?

The code below runs with no apparent errors, but despite I've looked for all possible causes I could think of, I couldn't find the reason why it gets the correct row iterated, marks it as processed ("Sim), and from the 2º iteration on, it gets the new row, but repeats the others already iterated over.
function formToData() {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var ss = sheet.getSheetByName("Form Responses 1");
var targetSheet = sheet.getSheetByName("Cadastro de Cliente");
var StartRow = 2;
var RowRange = ss.getLastRow() - StartRow + 1;
var WholeRange = ss.getRange(StartRow, 1, RowRange, 30);
var AllValues = WholeRange.getValues();
var message = "";
for (var i = 0; i < AllValues.length; i++) {
var currentRow = AllValues[i];
//if row has been sent, then continue to next iteration
if (currentRow[0] != "" && currentRow[29] != "Sim") {
//set the row to look at
var setRow = parseInt(i) + StartRow;
var data = currentRow[0];
var dataFormatted = Utilities.formatDate(data, SpreadsheetApp.getActive().getSpreadsheetTimeZone(), "dd/MM/yyyy', às 'HH:mm") + "hs";
//set HTML template for information
message +=
"<p><b>Data: </b>" + dataFormatted + "</p>" +
"<p><b>Unidade: </b>" + currentRow[1] + "</p>"
//mark row as "sent"
ss.getRange(setRow, 30).setValue("Sim");
var values = targetSheet.getRange("A:A").getValues();
var maxIndex = values.reduce(function (maxIndex, row, index) {
return row[0] === "" ? maxIndex : index;
}, 0);
targetSheet.getRange(maxIndex + 2, 1, 1, 30)
.setNumberFormat("#")
.setValues([currentRow]);
var sendTo = "email";
var subject = "Cadastro de cliente novo";
if (message) {
MailApp.sendEmail({
to: sendTo,
subject: subject,
name: "Comercial - Emape",
htmlBody: message,
});
}
}
}//For loop closes
}
I'd appreaciate if you could help me find the flaw.
I tried to replicate this behavior, however, on a static sheet the script performs as expected:
Set unmarked rows in column 30 as 'Sim'.
Copy these rows to a separate sheet starting from the first empty row (or whichever row that Column A is empty).
Given the names of the functions and the sheets, this may be a Sheet generated by a Google Form. These sheets are dynamic, and there is a possibility that the contents may change while your script is running, especially if users are allowed to edit responses.
As a workaround, I suggest to lock the form before running the script:
Also, check the contents of the sheet and form for any possibility that the "Sim" mark might be overwritten by new or edited form data, maybe the sheet is inserting 30 columns instead of 29 or less.

Adding a variable to a Vertical Panel in GAS

I am working on a master sheet which should have a UiApp embedded into the active sheet. I have tried coding the first part with the labels in the first row, but I am stuck with getting the names[i] to appear on the left hand side of the table under the Tasks label.
I intend to make check boxes appear for every name under each task label.
Here is the code below:
function showTable()
{
var ss = SpreadsheetApp.getActive();
var TL = ss.getRange('B3').getValue();
//SetFontWeight("bold") for TL < *This does not work either* >
var startDate = ss.getRange('B2').getValue();
var strStartDate = startDate.getDate() + "/" + (startDate.getMonth() + 1) + "/" + StartDate.getFullYear();
var counta = ss.getRange('B4').getValue();
var app = UiApp.createApplication().setHeight(375).setWidth(620)
.setTitle('This is ' + TL + "'s Team Tasks for " + strStartDate);
var panel = app.createAbsolutePanel().setId('panel').setHeight(355).setWidth(605)
.setStyleAttribute('background', 'lightCyan');
var names = ss.getRange('B11:B').getValues();
for (var i = 0; 0 < names.length; i++) **//This is the part that does not work**
{
var agents = app.createLabel(names[i]);
}
var handler1 = app.createServerHandler('btnCloseWindow');
var btnCloseWindow = app.createButton('Close Window').addClickHandler(handler1).setStyleAttribute('background', 'lightYellow');
handler1.addCallbackElement(panel);
var myLabel0 = app.createLabel('Tasks');
var myLabel1 = app.createLabel('HW');
var myLabel2 = app.createLabel('MU');
var myLabel3 = app.createLabel('MOV');
panel.add(myLabel0, 40, 12)
panel.add(myLabel1, 100, 12)
panel.add(myLabel2, 140, 12)
panel.add(myLabel3, 175, 12)
panel.add(agents, 40, 30)
panel.add(btnCloseWindow, 490, 320)
app.add(panel);
ss.show(app);
return app;
};
function btnCloseWindow(e)
{
var ss = SpreadsheetApp.getActive();
var app = UiApp.getActiveApplication();
app.close();
return app;
};
How do you make a vertical panel with the names?
The names are defined in col B11:B and the tasks are defined in the labels.
This is my first time making a UiApp so any help is very much appreciated!
Try it like this, I don't know how the whole think should look like but the part with the list of names is now right...
function showTable()
{
var ss = SpreadsheetApp.getActive();
var TL = ss.getRange('B3').getValue()
ss.getRange('B3').setFontWeight('bold')
var startDate = ss.getRange('B2').getValue();
var strStartDate = Utilities.formatDate(startDate, ss.getSpreadsheetTimeZone(), 'dd/mm/yyyy');
var counta = ss.getRange('B4').getValue();
var app = UiApp.createApplication().setHeight(375).setWidth(620)
.setTitle('This is ' + TL + "'s Team Tasks for " + strStartDate);
var panel = app.createAbsolutePanel().setId('panel').setHeight(355).setWidth(605)
.setStyleAttribute('background', 'lightCyan');
var handler1 = app.createServerHandler('btnCloseWindow');
var btnCloseWindow = app.createButton('Close Window').addClickHandler(handler1).setStyleAttribute('background', 'lightYellow');
handler1.addCallbackElement(panel);
var myLabel0 = app.createLabel('Tasks');
var myLabel1 = app.createLabel('HW');
var myLabel2 = app.createLabel('MU');
var myLabel3 = app.createLabel('MOV');
panel.add(myLabel0, 40, 12)
panel.add(myLabel1, 100, 12)
panel.add(myLabel2, 140, 12)
panel.add(myLabel3, 175, 12)
var names = ss.getRange('B11:B').getValues();
Logger.log(names)
for(i=0;i<names.length;++i){
Logger.log(names[i][0]);// names is a 2D array, you only want the first and only column
if(names[i][0]!=''){ // don't show if empty
panel.add(app.createLabel(names[i][0])); //add label to the panel
}
}
panel.add(btnCloseWindow, 490, 320)
app.add(panel);
ss.show(app);
};
var names = ss.getRange('B11:B').getValues();
Should be,
var names = ss.getRange('B11:B1').getValues();
How I researched,
var data = ss.getRange("b11:b1").getValues();
data.forEach(function(element,index,array){Logger.log(index + " " + element )});
Logs, "0 dog
1 cat
2 frog
3 fish
4 hamster
5 dog
6 cat
7 frog
8 fish
9 hamster
10 dog"

Resources