loop through sheet for unmarked value to update Google contacts - loops

I've got a working script that grabs the last row of a Google sheet and pushes the info into Google contacts.
var ss = SpreadsheetApp.getActiveSheet(); //var emailRow = ss.getRange('F2:F').getValues();
var emailRowNum = ss.getLastRow(); //var emailRowNum = emailRow.filter(String).length + 1;
function email() {
var emailNew = ss.getRange(emailRowNum,6).getValue(); //var emailNew = ss.getRange("F"+emailRowNum).getValues();
return emailNew;}
function givenName() {
var fname = ss.getRange(emailRowNum,4).getValue();
return fname;}
function lastName() {
var lname = ss.getRange(emailRowNum,5).getValue();
return lname;}
function loc() {
var street = ss.getRange(emailRowNum,8).getValue();
var city = ss.getRange(emailRowNum,9).getValue();
return street + " " + city;}
function phone() {
var phone = ss.getRange(emailRowNum,7).getValue();
return phone;}
function notes() {
var date = ss.getRange(emailRowNum,1).getValue();
var work = ss.getRange(emailRowNum,2).getValue();
var photo = ss.getRange(emailRowNum,3).getValue();
var site = ss.getRange(emailRowNum,12).getValue();
var find = ss.getRange(emailRowNum,10).getValue();
var referrer = ss.getRange(emailRowNum,11).getValue();
return (date + "\n\n" + work + "\n\n" + photo + "\n\n" + site + "\n\n" + find + " " + referrer + "\n\n-- eom --\n\n");}
function create() {
var fname = givenName();
var lname = lastName();
var contact = ContactsApp.createContact(fname, lname, email());
var group = ContactsApp.getContactGroup('emf');
group.addContact(contact);
var contacts = ContactsApp.getContactsByName(fname + ' ' + lname);
var setaddress = contacts[0].addAddress(ContactsApp.Field.HOME_ADDRESS,loc());
var setphone = contacts[0].addPhone(ContactsApp.Field.MAIN_PHONE,phone());
for (var i in contacts) {
contacts[i].setNotes(notes());
}
}
I'd like to modify it so that instead of grabbing the last row, it checks a column for a (not) value. If value is not found, then update Google contacts with that row's information.
Currently, I'm getting a 'Range not found' error ...
function info(){
var ss = SpreadsheetApp.getActiveSheet();
var data = ss.getRange("N1:N").getValues();
for(var n=0;n<data.length;n++){
if(data[n-1] != 'done'){
var email = ss.getRange("F"+n).getValue(); // Range not found error
var fname = ss.getRange("D"+n).getValue();
var lname = ss.getRange("E"+n).getValue();
var city = ss.getRange("I"+n).getValue();
var street = ss.getRange("H"+n).getValue();
var phone = ss.getRange("G"+n).getValue();
var date = ss.getRange("A"+n).getValue();
var work = ss.getRange("B"+n).getValue();
var photo = ss.getRange("C"+n).getValue();
var site = ss.getRange("L"+n).getValue();
var find = ss.getRange("J"+n).getValue();
var referrer = ss.getRange("K"+n).getValue();
var contact = ContactsApp.createContact(fname, lname, email);
var group = ContactsApp.getContactGroup('emf');
group.addContact(contact);
var contacts = ContactsApp.getContactsByName(fname + ' ' + lname);
var setaddress = contacts[0].addAddress(ContactsApp.Field.HOME_ADDRESS,street + " " + city);
var setphone = contacts[0].addPhone(ContactsApp.Field.MAIN_PHONE,phone);
for (var i in contacts) {
contacts[i].setNotes(date + "\n\n" + work + "\n\n" + photo + "\n\n" + site + "\n\n" + find + " " + referrer + "\n\n-- eom --\n\n");}
}
}
}

1 is the first row using A1Notation with getRange(). The first iteration is trying to getValue() of F0. Changing the n to start at 1 and n <= data.length should get the ranges you are looking for.
...
for(var n=1;n<=data.length;n++){
if(data[n-1] == 'done'){
var email = ss.getRange("F"+n).getValue(); // Range not found error
...
edit: One thing to note the var data = ss.getRange("N1:N").getValues(); range is going to loop over all 1000 default rows. This may not be ideal if your data set is significantly smaller than 1000 rows.

Related

Error: Cannot convert array of data to (class)

I'm still rather new to JS and GAS and I'm afraid I don't understand arrays real well yet. Your help is greatly appreciated.
Prior to this script, I have another script that pulls information from a list into a spreadsheet that is formatted to look like an invoice. The user then makes changes to the invoice and saves that changes back again to the exact same line that the data was pulled from. This is simply a way of updating changes to the invoice in a user-friendly format.
When I run my script below, I get an error saying that it "Cannot convert"...a list of the data in the array..." to (class)"...the name of the file.
I'm afraid I don't know enough about array classes to solve this problem. I looked for other questions and answers to this same problem, but if I found one, I didn't understand it well enough to apply it to my situation.
The error is happening on line 133 (oh, so close to running the whole script).
function overwriteInvoice() {
var ui = SpreadsheetApp.getUi();
var ss = SpreadsheetApp.getActive();
var sheet = ss.getSheetByName("Invoice");
//start change customer name into customer code
var customerName = sheet.getRange('I2').getValue();
var ccsheet = ss.getSheetByName("CustomerCodes");
var lastRow = ccsheet.getLastRow();
//check how many rows it found
Logger.log("lastRow: " + lastRow);
var lookUp = ccsheet.getRange(2, 1, lastRow, 3).getValues();
for (nn=0; nn<lookUp.length; ++nn) {
if (lookUp[nn][0] == customerName) {break}
}
var customerCode = lookUp[nn][1];
//check customer code to make sure it is returning correct info.
Logger.log("customerCode: " + customerCode);
//end change customer name into customer code
//start build array
var arrayOfData = [];
arrayOfData[0] = sheet.getRange('G2');//invoiceorquote
arrayOfData[1] = sheet.getRange('C10');//invoicequote number
arrayOfData[2] = sheet.getRange('C9');//date
arrayOfData[3] = customerCode;//customer code DON'T CHANGE THIS
arrayOfData[4] = sheet.getRange('I9'); //joblotcode
arrayOfData[5] = sheet.getRange('J27');//total
arrayOfData[6] = sheet.getRange('L32');//invoice paid
arrayOfData[7] = sheet.getRange('B30');//notes
arrayOfData[8] = sheet.getRange('B13');//area13;
arrayOfData[9] = sheet.getRange('D13');//description13;
arrayOfData[10] = sheet.getRange('I13');//quantity13;
arrayOfData[11] = sheet.getRange('J13');//units13;
arrayOfData[12] = sheet.getRange('K13');//pricePerUnit13;
arrayOfData[13] = sheet.getRange('L13');//subtotal13;
arrayOfData[14] = sheet.getRange('B14');//area14;
arrayOfData[15] = sheet.getRange('D14');//description14;
arrayOfData[16] = sheet.getRange('I14');//quantity14;
arrayOfData[17] = sheet.getRange('J14');//units14;
arrayOfData[18] = sheet.getRange('K14');//pricePerUnit14;
arrayOfData[19] = sheet.getRange('L14');//subtotal14;
arrayOfData[20] = sheet.getRange('B15');//area15;
arrayOfData[21] = sheet.getRange('D15');//description15;
arrayOfData[22] = sheet.getRange('I15');//quantity15;
arrayOfData[23] = sheet.getRange('J15');//units15 ;
arrayOfData[24] = sheet.getRange('K15');//pricePerUnit15 ;
arrayOfData[25] = sheet.getRange('L15');//subtotal15 ;
arrayOfData[26] = sheet.getRange('B16');//area16 ;
arrayOfData[27] = sheet.getRange('D16');//description16 ;
arrayOfData[28] = sheet.getRange('I16');//quantity16 ;
arrayOfData[29] = sheet.getRange('J16');//units16 ;
arrayOfData[30] = sheet.getRange('K16');//pricePerUnit16 ;
arrayOfData[31] = sheet.getRange('L16');//subtotal16 ;
arrayOfData[32] = sheet.getRange('B17');//area17 ;
arrayOfData[33] = sheet.getRange('D17');//description17 ;
arrayOfData[34] = sheet.getRange('I17');//quantity17 ;
arrayOfData[35] = sheet.getRange('J17');//units17 ;
arrayOfData[36] = sheet.getRange('K17');//pricePerUnit17 ;
arrayOfData[37] = sheet.getRange('L17');//subtotal17 ;
arrayOfData[38] = sheet.getRange('B18');//area18 ;
arrayOfData[39] = sheet.getRange('D18');//description18 ;
arrayOfData[40] = sheet.getRange('I18');//quantity18 ;
arrayOfData[41] = sheet.getRange('J18');//units18 ;
arrayOfData[42] = sheet.getRange('K18');//pricePerUnit18 ;
arrayOfData[43] = sheet.getRange('L18');//subtotal18 ;
arrayOfData[44] = sheet.getRange('B19');//area19 ;
arrayOfData[45] = sheet.getRange('D19');//description19 ;
arrayOfData[46] = sheet.getRange('I19');//quantity19 ;
arrayOfData[47] = sheet.getRange('J19');//units19 ;
arrayOfData[48] = sheet.getRange('K19');//pricePerUnit19 ;
arrayOfData[49] = sheet.getRange('L19');//subtotal19 ;
arrayOfData[50] = sheet.getRange('B20');//area20 ;
arrayOfData[51] = sheet.getRange('D20');//description20 ;
arrayOfData[52] = sheet.getRange('I20');//quantity20 ;
arrayOfData[53] = sheet.getRange('J20');//units20 ;
arrayOfData[54] = sheet.getRange('K20');//pricePerUnit20 ;
arrayOfData[55] = sheet.getRange('L20');//subtotal20 ;
arrayOfData[56] = sheet.getRange('B21');//area21 ;
arrayOfData[57] = sheet.getRange('D21');//description21 ;
arrayOfData[58] = sheet.getRange('I21');//quantity21 ;
arrayOfData[59] = sheet.getRange('J21');//units21 ;
arrayOfData[60] = sheet.getRange('K21');//pricePerUnit21 ;
arrayOfData[61] = sheet.getRange('L21');//subtotal21 ;
arrayOfData[62] = sheet.getRange('B22');//area22 ;
arrayOfData[63] = sheet.getRange('D22');//description22 ;
arrayOfData[64] = sheet.getRange('I22');//quantity22 ;
arrayOfData[65] = sheet.getRange('J22');//units22 ;
arrayOfData[66] = sheet.getRange('K22');//pricePerUnit22 ;
arrayOfData[67] = sheet.getRange('L22');//subtotal22 ;
arrayOfData[68] = sheet.getRange('B23');//area23 ;
arrayOfData[69] = sheet.getRange('D23');//description23 ;
arrayOfData[70] = sheet.getRange('I23');//quantity23 ;
arrayOfData[71] = sheet.getRange('J23');//units23 ;
arrayOfData[72] = sheet.getRange('K23');//pricePerUnit23 ;
arrayOfData[73] = sheet.getRange('L23');//subtotal23 ;
arrayOfData[74] = sheet.getRange('B24');//area24 ;
arrayOfData[75] = sheet.getRange('D24');//description24 ;
arrayOfData[76] = sheet.getRange('I24');//quantity24 ;
arrayOfData[77] = sheet.getRange('J24');//units24 ;
arrayOfData[78] = sheet.getRange('K24');//pricePerUnit24 ;
arrayOfData[79] = sheet.getRange('L24');//subtotal24 ;
//end build array
//start find correct row
var searchFor = sheet.getRange("C10").getValue();
var invoiceRecord = ss.getSheetByName("InvoiceRecord");
var lastRow = invoiceRecord.getLastRow();
//verify last row
Logger.log("lastRow: " + lastRow);
//create an array of values to search
var searchArea = invoiceRecord.getRange(4, 1, lastRow, 4).getValues();
//start search the array
for (jj=0; jj<searchArea.length; ++jj) {
if (searchArea[jj][1]==searchFor) {break};
}
var copyToRow = searchArea[jj];
//end find the correct row
//ERROR!
invoiceRecord.getRange(copyToRow, 1, 1, arrayOfData.length).setValues(arrayOfData);
//ERROR!
ui.alert('Invoice Changes Saved', ui.ButtonSet.OK);
}
Thank you so much for your help.
Update
I am so frustrated by this problem that I decided to take a different tact to narrow down the cause. I removed the array entirely and assigned each value to a variable in order to see if the array is the problem. Apparently, it is not. The results when I run this code now say error: "Cannot convert NaN to (class). (line 140, file "OverwriteExistingInvoice")"
function overwriteInvoice() {
var ui = SpreadsheetApp.getUi();
var ss = SpreadsheetApp.getActive();
var sheet = ss.getSheetByName("Invoice");
//start change customer name into customer code
var customerName = sheet.getRange('I2').getValue();
var ccsheet = ss.getSheetByName("CustomerCodes");
var lastRow = ccsheet.getLastRow();
//check how many rows it found
Logger.log("lastRow: " + lastRow);
var lookUp = ccsheet.getRange(2, 1, lastRow-2, 3).getValues();
var nn=0
for (nn=0; nn<lookUp.length; ++nn) {
if (lookUp[nn][0] == customerName) {break}
}
var customerCode = lookUp[nn][1];
//check customer code to make sure it is returning correct info.
Logger.log("customerCode: " + customerCode);
//end change customer name into customer code
//start build array
var column0 = sheet.getRange('G2').getValue();//invoiceorquote
var column1 = sheet.getRange('C10').getValue();//invoicequote number
var column2 = sheet.getRange('C9').getValue();//date
var column3 = customerCode;//customer code DON'T CHANGE THIS
var column4 = sheet.getRange('I9').getValue(); //joblotcode
var column5 = sheet.getRange('J27').getValue();//total
var column6 = sheet.getRange('L32').getValue();//invoice paid
var column7 = sheet.getRange('B30').getValue();//notes
var column8 = sheet.getRange('B13').getValue();//area13;
var column9 = sheet.getRange('D13').getValue();//description13;
var column10 = sheet.getRange('I13').getValue();//quantity13;
var column11 = sheet.getRange('J13').getValue();//units13;
var column12 = sheet.getRange('K13').getValue();//pricePerUnit13;
var column13 = sheet.getRange('L13').getValue();//subtotal13;
var column14 = sheet.getRange('B14').getValue();//area14;
var column15 = sheet.getRange('D14').getValue();//description14;
var column16 = sheet.getRange('I14').getValue();//quantity14;
var column17 = sheet.getRange('J14').getValue();//units14;
var column18 = sheet.getRange('K14').getValue();//pricePerUnit14;
var column19 = sheet.getRange('L14').getValue();//subtotal14;
var column20 = sheet.getRange('B15').getValue();//area15;
var column21 = sheet.getRange('D15').getValue();//description15;
var column22 = sheet.getRange('I15').getValue();//quantity15;
var column23 = sheet.getRange('J15').getValue();//units15 ;
var column24 = sheet.getRange('K15').getValue();//pricePerUnit15 ;
var column25 = sheet.getRange('L15').getValue();//subtotal15 ;
var column26 = sheet.getRange('B16').getValue();//area16 ;
var column27 = sheet.getRange('D16').getValue();//description16 ;
var column28 = sheet.getRange('I16').getValue();//quantity16 ;
var column29 = sheet.getRange('J16').getValue();//units16 ;
var column30 = sheet.getRange('K16').getValue();//pricePerUnit16 ;
var column31 = sheet.getRange('L16').getValue();//subtotal16 ;
var column32 = sheet.getRange('B17').getValue();//area17 ;
var column33 = sheet.getRange('D17').getValue();//description17 ;
var column34 = sheet.getRange('I17').getValue();//quantity17 ;
var column35 = sheet.getRange('J17').getValue();//units17 ;
var column36 = sheet.getRange('K17').getValue();//pricePerUnit17 ;
var column37 = sheet.getRange('L17').getValue();//subtotal17 ;
var column38 = sheet.getRange('B18').getValue();//area18 ;
var column39 = sheet.getRange('D18').getValue();//description18 ;
var column40 = sheet.getRange('I18').getValue();//quantity18 ;
var column41 = sheet.getRange('J18').getValue();//units18 ;
var column42 = sheet.getRange('K18').getValue();//pricePerUnit18 ;
var column43 = sheet.getRange('L18').getValue();//subtotal18 ;
var column44 = sheet.getRange('B19').getValue();//area19 ;
var column45 = sheet.getRange('D19').getValue();//description19 ;
var column46 = sheet.getRange('I19').getValue();//quantity19 ;
var column47 = sheet.getRange('J19').getValue();//units19 ;
var column48 = sheet.getRange('K19').getValue();//pricePerUnit19 ;
var column49 = sheet.getRange('L19').getValue();//subtotal19 ;
var column50 = sheet.getRange('B20').getValue();//area20 ;
var column51 = sheet.getRange('D20').getValue();//description20 ;
var column52 = sheet.getRange('I20').getValue();//quantity20 ;
var column53 = sheet.getRange('J20').getValue();//units20 ;
var column54 = sheet.getRange('K20').getValue();//pricePerUnit20 ;
var column55 = sheet.getRange('L20').getValue();//subtotal20 ;
var column56 = sheet.getRange('B21').getValue();//area21 ;
var column57 = sheet.getRange('D21').getValue();//description21 ;
var column58 = sheet.getRange('I21').getValue();//quantity21 ;
var column59 = sheet.getRange('J21').getValue();//units21 ;
var column60 = sheet.getRange('K21').getValue();//pricePerUnit21 ;
var column61 = sheet.getRange('L21').getValue();//subtotal21 ;
var column62 = sheet.getRange('B22').getValue();//area22 ;
var column63 = sheet.getRange('D22').getValue();//description22 ;
var column64 = sheet.getRange('I22').getValue();//quantity22 ;
var column65 = sheet.getRange('J22').getValue();//units22 ;
var column66 = sheet.getRange('K22').getValue();//pricePerUnit22 ;
var column67 = sheet.getRange('L22').getValue();//subtotal22 ;
var column68 = sheet.getRange('B23').getValue();//area23 ;
var column69 = sheet.getRange('D23').getValue();//description23 ;
var column70 = sheet.getRange('I23').getValue();//quantity23 ;
var column71 = sheet.getRange('J23').getValue();//units23 ;
var column72 = sheet.getRange('K23').getValue();//pricePerUnit23 ;
var column73 = sheet.getRange('L23').getValue();//subtotal23 ;
var column74 = sheet.getRange('B24').getValue();//area24 ;
var column75 = sheet.getRange('D24').getValue();//description24 ;
var column76 = sheet.getRange('I24').getValue();//quantity24 ;
var column77 = sheet.getRange('J24').getValue();//units24 ;
var column78 = sheet.getRange('K24').getValue();//pricePerUnit24 ;
var column79 = sheet.getRange('L24').getValue();//subtotal24 ;
//end build array
//start find correct row
var searchFor = sheet.getRange("C10").getValue();
var invoiceRecord = ss.getSheetByName("InvoiceRecord");
var lastRow = invoiceRecord.getLastRow();
//verify last row
Logger.log("lastRow: " + lastRow);
//create an array of values to search
var searchArea = invoiceRecord.getRange(4, 1, lastRow, 4).getValues();
//start search the array
var jj=0
for (jj=0; jj<searchArea.length; ++jj) {
if (searchArea[jj][1]==searchFor) {break};
}
Logger.log('jj value: ' + jj);
var copyToRow = searchArea[jj];
Logger.log('copyToRow: ' + copyToRow);
//end find correct row
//start copy over same entry
//error on next line
invoiceRecord.getRange(copyToRow-1, 1).setValue(column0);
//error on above line
ui.alert('Invoice Changes Saved', ui.ButtonSet.OK);
}
I really appreciate all the help thus far. I am about to tear my hair out!
UPDATE
This is the code that finally worked correctly.
function overwriteInvoice() {
var ui = SpreadsheetApp.getUi();
var ss = SpreadsheetApp.getActive();
var sheet = ss.getSheetByName("Invoice");
//start change customer name into customer code
var customerName = sheet.getRange('I2').getValue();
var ccsheet = ss.getSheetByName("CustomerCodes");
var lastRow = ccsheet.getLastRow();
//check how many rows it found
Logger.log("lastRow: " + lastRow);
var lookUp = ccsheet.getRange(2, 1, lastRow-2, 3).getValues();
var nn=0
for (nn=0; nn<lookUp.length; ++nn) {
if (lookUp[nn][0] == customerName) {break}
}
var customerCode = lookUp[nn][1];
//check customer code to make sure it is returning correct info.
Logger.log("customerCode: " + customerCode);
//end change customer name into customer code
//start build array
var arrayOfData = [];
arrayOfData[0] = sheet.getRange('G2').getValue();//invoiceorquote
arrayOfData[1] = sheet.getRange('C10').getValue();//invoicequote number
arrayOfData[2] = sheet.getRange('C9').getValue();//date
arrayOfData[3] = customerCode;//customer code DON'T CHANGE THIS
arrayOfData[4] = sheet.getRange('I9').getValue(); //joblotcode
arrayOfData[5] = sheet.getRange('J27').getValue();//total
arrayOfData[6] = sheet.getRange('L32').getValue();//invoice paid
arrayOfData[7] = sheet.getRange('B30').getValue();//notes
arrayOfData[8] = sheet.getRange('B13').getValue();//area13;
arrayOfData[9] = sheet.getRange('D13').getValue();//description13;
arrayOfData[10] = sheet.getRange('I13').getValue();//quantity13;
arrayOfData[11] = sheet.getRange('J13').getValue();//units13;
arrayOfData[12] = sheet.getRange('K13').getValue();//pricePerUnit13;
arrayOfData[13] = sheet.getRange('L13').getValue();//subtotal13;
arrayOfData[14] = sheet.getRange('B14').getValue();//area14;
arrayOfData[15] = sheet.getRange('D14').getValue();//description14;
arrayOfData[16] = sheet.getRange('I14').getValue();//quantity14;
arrayOfData[17] = sheet.getRange('J14').getValue();//units14;
arrayOfData[18] = sheet.getRange('K14').getValue();//pricePerUnit14;
arrayOfData[19] = sheet.getRange('L14').getValue();//subtotal14;
arrayOfData[20] = sheet.getRange('B15').getValue();//area15;
arrayOfData[21] = sheet.getRange('D15').getValue();//description15;
arrayOfData[22] = sheet.getRange('I15').getValue();//quantity15;
arrayOfData[23] = sheet.getRange('J15').getValue();//units15 ;
arrayOfData[24] = sheet.getRange('K15').getValue();//pricePerUnit15 ;
arrayOfData[25] = sheet.getRange('L15').getValue();//subtotal15 ;
arrayOfData[26] = sheet.getRange('B16').getValue();//area16 ;
arrayOfData[27] = sheet.getRange('D16').getValue();//description16 ;
arrayOfData[28] = sheet.getRange('I16').getValue();//quantity16 ;
arrayOfData[29] = sheet.getRange('J16').getValue();//units16 ;
arrayOfData[30] = sheet.getRange('K16').getValue();//pricePerUnit16 ;
arrayOfData[31] = sheet.getRange('L16').getValue();//subtotal16 ;
arrayOfData[32] = sheet.getRange('B17').getValue();//area17 ;
arrayOfData[33] = sheet.getRange('D17').getValue();//description17 ;
arrayOfData[34] = sheet.getRange('I17').getValue();//quantity17 ;
arrayOfData[35] = sheet.getRange('J17').getValue();//units17 ;
arrayOfData[36] = sheet.getRange('K17').getValue();//pricePerUnit17 ;
arrayOfData[37] = sheet.getRange('L17').getValue();//subtotal17 ;
arrayOfData[38] = sheet.getRange('B18').getValue();//area18 ;
arrayOfData[39] = sheet.getRange('D18').getValue();//description18 ;
arrayOfData[40] = sheet.getRange('I18').getValue();//quantity18 ;
arrayOfData[41] = sheet.getRange('J18').getValue();//units18 ;
arrayOfData[42] = sheet.getRange('K18').getValue();//pricePerUnit18 ;
arrayOfData[43] = sheet.getRange('L18').getValue();//subtotal18 ;
arrayOfData[44] = sheet.getRange('B19').getValue();//area19 ;
arrayOfData[45] = sheet.getRange('D19').getValue();//description19 ;
arrayOfData[46] = sheet.getRange('I19').getValue();//quantity19 ;
arrayOfData[47] = sheet.getRange('J19').getValue();//units19 ;
arrayOfData[48] = sheet.getRange('K19').getValue();//pricePerUnit19 ;
arrayOfData[49] = sheet.getRange('L19').getValue();//subtotal19 ;
arrayOfData[50] = sheet.getRange('B20').getValue();//area20 ;
arrayOfData[51] = sheet.getRange('D20').getValue();//description20 ;
arrayOfData[52] = sheet.getRange('I20').getValue();//quantity20 ;
arrayOfData[53] = sheet.getRange('J20').getValue();//units20 ;
arrayOfData[54] = sheet.getRange('K20').getValue();//pricePerUnit20 ;
arrayOfData[55] = sheet.getRange('L20').getValue();//subtotal20 ;
arrayOfData[56] = sheet.getRange('B21').getValue();//area21 ;
arrayOfData[57] = sheet.getRange('D21').getValue();//description21 ;
arrayOfData[58] = sheet.getRange('I21').getValue();//quantity21 ;
arrayOfData[59] = sheet.getRange('J21').getValue();//units21 ;
arrayOfData[60] = sheet.getRange('K21').getValue();//pricePerUnit21 ;
arrayOfData[61] = sheet.getRange('L21').getValue();//subtotal21 ;
arrayOfData[62] = sheet.getRange('B22').getValue();//area22 ;
arrayOfData[63] = sheet.getRange('D22').getValue();//description22 ;
arrayOfData[64] = sheet.getRange('I22').getValue();//quantity22 ;
arrayOfData[65] = sheet.getRange('J22').getValue();//units22 ;
arrayOfData[66] = sheet.getRange('K22').getValue();//pricePerUnit22 ;
arrayOfData[67] = sheet.getRange('L22').getValue();//subtotal22 ;
arrayOfData[68] = sheet.getRange('B23').getValue();//area23 ;
arrayOfData[69] = sheet.getRange('D23').getValue();//description23 ;
arrayOfData[70] = sheet.getRange('I23').getValue();//quantity23 ;
arrayOfData[71] = sheet.getRange('J23').getValue();//units23 ;
arrayOfData[72] = sheet.getRange('K23').getValue();//pricePerUnit23 ;
arrayOfData[73] = sheet.getRange('L23').getValue();//subtotal23 ;
arrayOfData[74] = sheet.getRange('B24').getValue();//area24 ;
arrayOfData[75] = sheet.getRange('D24').getValue();//description24 ;
arrayOfData[76] = sheet.getRange('I24').getValue();//quantity24 ;
arrayOfData[77] = sheet.getRange('J24').getValue();//units24 ;
arrayOfData[78] = sheet.getRange('K24').getValue();//pricePerUnit24 ;
arrayOfData[79] = sheet.getRange('L24').getValue();//subtotal24 ;
//end build array
//start find correct row
var searchFor = sheet.getRange("C10").getValue();
var invoiceRecord = ss.getSheetByName("InvoiceRecord");
var lastRow = invoiceRecord.getLastRow();
//verify last row
Logger.log("lastRow: " + lastRow);
//create an array of values to search
var searchArea = invoiceRecord.getRange(4, 1, lastRow, 4).getValues();
//start search the array
var jj=0
for (jj=0; jj<searchArea.length; ++jj) {
if (searchArea[jj][1]==searchFor) {break};
}
Logger.log('jj value: ' + jj);
var copyToRow = jj+4;
Logger.log('copyToRow: ' + copyToRow);
//end find correct row
//start copy over the same entry
var outerArray = [];
outerArray.push(arrayOfData);
invoiceRecord.getRange(copyToRow, 1, 1, 80).setValues(outerArray);
ui.alert('Invoice Changes Saved', ui.ButtonSet.OK);
}
Add the getValue() method to all of the lines with:
arrayOfData[x] = sheet.getRange('XX');//What to get
Right now, a range object is being put into the array, and not a value.
Should be:
arrayOfData[x] = sheet.getRange('XX').getValue();//Note
Create an outer array, and put arrayOfData into it:
var outerArray = [];
outerArray.push(arrayOfData);
//getRange(Start row, start column, number of rows, number of columns)
invoiceRecord.getRange(copyToRow, 1, outerArray.length, outerArray[0].length)
.setValues(outerArray);
The setValues() method must have a two dimensional array. If this is for just one row of data, then there will only be one inner array.

Last row writes twice

I had a broken code which I have managed to get working (The original question is here https://stackoverflow.com/questions/29127005/multiple-spreadsheet-rows-from-multiple-forms), but the loop writes the last line twice and my coding skills are not good enough for me to work out why...yet!
Can anyone shed any light on what I have/haven't included?
function InsertDataInSheet(e) //Function to insert data into spreadsheet on clicking submit
{
var app = UiApp.getActiveApplication();
//get number of rows to input
var num = parseInt(e.parameter.table_tag);
var num = num+1;
//set increment step through
for (var i = 1; i < num ; i++ ) {
//Declare varialbe fields to collect data from
var user = Session.getActiveUser().getEmail();
var date = e.parameter['DateBox'+i];
var location = e.parameter['LocationListBox'+i];
var source = e.parameter['SourceListBox'+i];
var reporter = e.parameter['ReporterTextBox'+i];
var priority = e.parameter['PriorityListBox'+i];
var hazard = e.parameter['HazardListBox'+i];
var details = e.parameter['DetailsTextBox'+i];
var description = e.parameter['DescriptionTextBox'+i];
var timeStamp = new Date();
//Decide date that this needs to be closed by
if (priority === '02 - WITHIN 24-48 HOURS') {
var dateTemp = new Date(date);
dateTemp.setDate(dateTemp.getDate()+2);
var actiondate = dateTemp;
}
if (priority === '03 - WITHIN 1 WEEK') {
var dateTemp = new Date(date);
dateTemp.setDate(dateTemp.getDate()+7);
var actiondate = dateTemp;
}
//establish email addresses
//Declare correct range to obtain values
var LocationSheet = SpreadsheetApp.openById(itemSpreadsheetKey).getSheetByName("LocationCodes")
//Start with central maintenance department
var email00 = LocationSheet.getRange(33,5).getValue()
//followed by other emails as they appear
var email01 = LocationSheet.getRange(3,5).getValue();
var email02 = LocationSheet.getRange(4,5).getValue();
//declare the correct Depots to check
var LocationSheet = SpreadsheetApp.openById(itemSpreadsheetKey).getSheetByName("LocationCodes");
var depot01 = LocationSheet.getRange(3,4).getValue();
var depot02 = LocationSheet.getRange(4,4).getValue();
//if source is recorded as '08 - Maitenance Request System', the recipient is maintenance deparment
if (source === "08 - Maintenance Request System"){
var recipient = email00;
//or depots as listed
} else if(location === depot01){
var recipient = email01;
} else if(location === depot02){
var recipient = email02; }
else {
//and send an email to the error catch all if no code supplied
var recipient = email00; //change as necessary
}
var sheet = SpreadsheetApp.openById(itemSpreadsheetKey).getSheetByName('LOG');
var lastRow = sheet.getLastRow();
var lrp1 = lastRow+1
var targetRange = sheet.getRange(lastRow+1, 1, 1, 12).setValues([[timeStamp,date,source,location,reporter,user,hazard,details,description,priority,recipient,actiondate]]);
//Notification Page
app = UiApp.getActiveApplication().remove(0);
app.createVerticalPanel()
.setId('info')
.setVisible(true)
.setStyleAttribute('left', 10)
.setStyleAttribute('top', 10)
.setStyleAttribute('zIndex', '1')
.setStyleAttribute('position', 'fixed')
.setStyleAttribute('background', 'white')
.setHeight('400px')
.setStyleAttribute('text-align', 'center')
.setBorderWidth(1)
.setWidth('500px');
app.add(app.createLabel('Your submission has been added to the database & the Site Coordinator will be emailed with notification.'));
// app.add(app.createLabel('The details you entered were recorded as:'));
// app.add(app.createLabel('Username: [' +user+ '] Timestamp: [' +timeStamp+ '] Reporter: [' +reporter+ ']'));
// app.add(app.createLabel('Location: [' +location+ '] Hazard: [' +hazard+ '] Source: [' +source+ ']'));
// app.add(app.createLabel('You listed the priority as: [' +priority+ '], please be aware this may not be the same priority given by the Site Coordinator responsible.'));
app.add(app.createLabel('Please refresh this page to submit more entries'));
return app.close();
}
var sheet = SpreadsheetApp.openById(itemSpreadsheetKey).getSheetByName("LOG");
var lastRow = sheet.getLastRow();
var lrp1 = lastRow+1
//Amend [getRange(lastRow+1, 1, 1, **)] integer to reflet number of headers being written if more added
var targetRange = sheet.getRange(lastRow+1, 1, 1, 12).setValues([[timeStamp,date,source,location,reporter,user,hazard,details,description,priority,recipient,actiondate]]);
var Body = 'A new [' +source+ '] log entry has been recorded at [' +location+ '], listed as [' + hazard+ ']. This form was submitted by [' +user+ '] with the timestamp [' +timeStamp+ '].'
}
The reason why it is adding last row twice is that because the following code is repeated after the loop:
var sheet = SpreadsheetApp.openById(itemSpreadsheetKey).getSheetByName("LOG");
var lastRow = sheet.getLastRow();
var lrp1 = lastRow+1
//Amend [getRange(lastRow+1, 1, 1, **)] integer to reflet number of headers being written if more added
var targetRange = sheet.getRange(lastRow+1, 1, 1, 12).setValues([[timeStamp,date,source,location,reporter,user,hazard,details,description,priority,recipient,actiondate]]);
var Body = 'A new [' +source+ '] log entry has been recorded at [' +location+ '], listed as [' + hazard+ ']. This form was submitted by [' +user+ '] with the timestamp [' +timeStamp+ '].'
Hope that helps!
Thanks KRR, I sorted it with that and the moving of one of the "}" from after the notification page to just before it. The corrected code is:
var sheet = SpreadsheetApp.openById(itemSpreadsheetKey).getSheetByName('LOG');
var lastRow = sheet.getLastRow();
var lrp1 = lastRow+1
var targetRange = sheet.getRange(lastRow+1, 1, 1, 12).setValues([[timeStamp,date,source,location,reporter,user,hazard,details,description,priority,recipient,actiondate]]);
}
///// TRAIL NOTIFICATION PAGE FROM WITHIN SYSTEM FROM HERE /////
app = UiApp.getActiveApplication().remove(0);
app.createVerticalPanel()
.setId('info')
.setVisible(true)
.setStyleAttribute('left', 10)
.setStyleAttribute('top', 10)
.setStyleAttribute('zIndex', '1')
.setStyleAttribute('position', 'fixed')
.setStyleAttribute('background', 'white')
.setHeight('400px')
.setStyleAttribute('text-align', 'center')
.setBorderWidth(1)
.setWidth('500px');
app.add(app.createLabel('Your submission has been added to the database & the Site Coordinator will be emailed with notification.'));
// app.add(app.createLabel('The details you entered were recorded as:'));
// app.add(app.createLabel('Username: [' +user+ '] Timestamp: [' +timeStamp+ '] Reporter: [' +reporter+ ']'));
// app.add(app.createLabel('Location: [' +location+ '] Hazard: [' +hazard+ '] Source: [' +source+ ']'));
// app.add(app.createLabel('You listed the priority as: [' +priority+ '], please be aware this may not be the same priority given by the Site Coordinator responsible.'));
app.add(app.createLabel('Please refresh this page to submit more entries'));
return app.close();
}

Selects from multiple tables for Activities feed

I have a social app for which I am trying to create a friend activities feed using Azure Sql Server.
I have 3 tables I want to select from:
Songs
-createdAt
-id
-userId
-trackName
-etc
Comments
-createdAt
-id
-userId
-songId
-text
Likes
-createdAt
-id
-userId
-songId
I have the users that the current user is following stored in an array named 'follows'.
How do I go about selecting the 40 most recent items from those 3 tables where userId in each table is in the follows array?
Edit:
function getActivities(userId) {
var deferred = Q.defer();
var follows = [];
getFollowing(userId).then(function (results) {
follows.push(userId);
_.each(results, function (user) {
follows.push(user.toUserId);
});
return;
}).then(function () {
var stringified = "'" + follows.join("','") + "'";
var queryString = "SELECT * FROM comments, songs, likes WHERE comments.userId IN (" + stringified + ") OR songs.userId IN (" + stringified +") OR likes.userId IN (" + stringified + ")";
var params = [];
return sqlQuery(queryString, params);
}).then(function (results) {
console.log('Activities: ', results);
deferred.resolve(results);
}, function (error) {
console.log('Error: ', error.message);
deferred.reject(error.message);
});
return deferred.promise;
}
Alright, so I dug into JOINS a little more and realized how easy it actually is once you wrap your head around it. Here is what I did to complete this:
var queryString = "SELECT TOP 50 follows.id AS followId, follows.toUserId AS followToUserId, follows.fromUserId AS followFromUserId, comments.text AS commentText, profiles.userId, profiles.username, profiles.name, profiles.profileImage, songs.trackId, songs.trackName, songs.artistName, songs.collectionName, songs.artworkUrl100, songs.caption, songs.id AS songId, activities.id AS activityId, activities.type AS activityType, activities.objectId AS activityObjectId, activities.parentType AS activityParentType, activities.parentId AS activityParentId, activities.__createdAt AS activityCreatedAt FROM activities ";
queryString += "INNER JOIN profiles ON (profiles.userId = activities.userId) ";
queryString += "LEFT JOIN songs ON (songs.id = activities.objectId AND activities.type = 'songs') OR (songs.id = activities.parentId AND activities.parentType = 'songs') ";
queryString += "LEFT JOIN comments ON (activities.type = 'comments' AND comments.id = activities.objectId) ";
queryString += "LEFT JOIN follows ON (activities.type = 'followed' AND activities.userid = follows.fromUserId) ";
queryString += "WHERE activities.userId IN (SELECT follows.toUserId AS userId FROM follows WHERE follows.fromUserId = ? AND follows.isFollowed = 'true') ";
queryString += "ORDER BY activities.__createdAt DESC";
var params = [userId];
mssql.query(queryString, params, {
success: function (results) {
_.each(results, function (result) {
//Remove columns with null or undefined values
for (var i in result) {
if (result[i] === null || result[i] === undefined) {
delete result[i];
}
}
});
response.send(200, results);
},
error: function (error) {
response.send(400, error.message);
}
});

Keep input data after posting

I have an issue with a long form I'm doing. If a user fills out the form, and validation is invalid, it erases everything in the form, and doesn't keep the data in the valid forms. How would I make it so it doesn't erase the data upon pressing submit?
#{
Layout = "~/Pages/_SiteLayout.cshtml";
var db = Database.Open("mtarplspd");
Validation.RequireField("TypeOfIncident", "Type of incident is required.");
Validation.RequireField("FirstName", "Firstname is required.");
Validation.RequireField("LastName", "Lastname is required.");
Validation.RequireField("Rank", "Rank is required.");
Validation.RequireField("Badge", "Badge is required.");
Validation.RequireField("SuspectFirstName", "Suspect firstname is required.");
Validation.RequireField("SuspectLastName", "Suspect lastname is required.");
Validation.RequireField("SuspectCharges", "Suspect charges is required.");
Validation.RequireField("Description", "Description of the incident is required.");
var typeOfIncident = "";
var firstName = "";
var lastName = "";
var rank = "";
var badge = "";
var suspectFirstName = "";
var suspectLastName = "";
var suspectAddress = "";
var suspectCharges = "";
var suspectVehicleModel = "";
var suspectVehiclePlates = "";
var suspectVehicleColor = "";
var officersInvolved = "";
var description = "";
var departmentsInvolved = "";
var firearmDischarged = "";
var typeOfFirearm = "";
var roundsFired = "";
var reason = "";
var userId = 0;
var dateTime = "";
var ErrorMessage = "";
if (IsPost) {
typeOfIncident = Request.Form["TypeOfIncident"];
firstName = Request.Form["FirstName"];
lastName = Request.Form["LastName"];
rank = Request.Form["Rank"];
badge = Request.Form["Badge"];
suspectFirstName = Request.Form["SuspectFirstName"];
suspectLastName = Request.Form["SuspectLastName"];
suspectAddress = Request.Form["SuspectAddress"];
suspectCharges = Request.Form["SuspectCharges"];
suspectVehicleModel = Request.Form["SuspectVehicleModel"];
suspectVehiclePlates = Request.Form["SuspectVehiclePlates"];
suspectVehicleColor = Request.Form["SuspectVehicleColor"];
officersInvolved = Request.Form["OfficersInvolved"];
description = Request.Unvalidated["Description"];
departmentsInvolved = Request.Form["DepartmentsInvolved"];
firearmDischarged = Request.Form["FirearmDischarged"];
typeOfFirearm = Request.Form["TypeOfFirearm"];
roundsFired = Request.Form["RoundsFired"];
reason = Request.Form["Reason"];
userId = WebSecurity.CurrentUserId;
dateTime = DateTime.Now.ToString("d MMM yyyy h:mm:ss");
if (ErrorMessage == "" && Validation.IsValid()) {
var insert = "INSERT INTO Reports (Type, FirstName, LastName, Rank, Badge, SuspectFirstName, SuspectLastName, SuspectAddress, SuspectCharges, SuspectVehicleModel, SuspectVehiclePlates, SuspectVehicleColor, OfficersInvolved, Description, DepartmentsInvolved, FirearmDischarged, TypeOfFirearm, RoundsFired, Reason, UserId, DateTime)" + "VALUES (#0, #1, #2, #3, #4, #5, #6, #7, #8, #9, #10, #11, #12, #13, #14, #15, #16, #17, #18, #19, #20)";
db.Execute(insert, typeOfIncident, firstName, lastName, rank, badge, suspectFirstName, suspectLastName, suspectAddress, suspectCharges, suspectVehicleModel, suspectVehiclePlates, suspectVehicleColor, officersInvolved, description, departmentsInvolved, firearmDischarged, typeOfFirearm, roundsFired, reason, userId, dateTime);
Response.Redirect("~/Pages/Reports/Incident/Default");
}
}
}
You need to restore the value for each form control, eg:
<input type="text" name="FirstName" value="#Request.Form["FirstName"]" />

Script to export layer coordinates to excel

I have found a script that export my layers coordinates form photoshop CS5 to XML
I hope somebody here can help me to edit that script to record coordinates to xls file?
Also if is possible to have each coordinates on separate row will be great.
Below is script I want to modify to do what I need.
//
// This script exports extended layer.bounds information to [psd_file_name].xml
// by pattesdours
//
function docCheck() {
// ensure that there is at least one document open
if (!documents.length) {
alert('There are no documents open.');
return; // quit
}
}
docCheck();
var originalRulerUnits = preferences.rulerUnits;
preferences.rulerUnits = Units.PIXELS;
var docRef = activeDocument;
var docWidth = docRef.width.value;
var docHeight = docRef.height.value;
var mySourceFilePath = activeDocument.fullName.path + "/";
// Code to get layer index / descriptor
//
cTID = function(s) { return app.charIDToTypeID(s); };
sTID = function(s) { return app.stringIDToTypeID(s); };
function getLayerDescriptor (doc, layer) {
var ref = new ActionReference();
ref.putEnumerated(cTID("Lyr "), cTID("Ordn"), cTID("Trgt"));
return executeActionGet(ref)
};
function getLayerID(doc, layer) {
var d = getLayerDescriptor(doc, layer);
return d.getInteger(cTID('LyrI'));
};
var stackorder = 0;
// function from Xbytor to traverse all layers
traverseLayers = function(doc, ftn, reverse) {
function _traverse(doc, layers, ftn, reverse) {
var ok = true;
for (var i = 1; i <= layers.length && ok != false; i++) {
var index = (reverse == true) ? layers.length-i : i - 1;
var layer = layers[index];
if (layer.typename == "LayerSet") {
ok = _traverse(doc, layer.layers, ftn, reverse);
} else {
stackorder = stackorder + 1;
ok = ftn(doc, layer, stackorder);
}
}
return ok;
};
return _traverse(doc, doc.layers, ftn, reverse);
};
// create a string to hold the data
var str ="";
// class using a contructor
function cLayer(doc, layer) {
//this.layerID = Stdlib.getLayerID(doc, layer);
this.layerID = getLayerID(doc, layer);
//alert("layer ID: " + this.layerID);
this.layerWidth = layer.bounds[2].value - layer.bounds[0].value;
this.layerHeight = layer.bounds[3].value - layer.bounds[1].value;
// these return object coordinates relative to canvas
this.upperLeftX = layer.bounds[0].value;
this.upperLeftY = layer.bounds[1].value;
this.upperCenterX = this.layerWidth / 2 + layer.bounds[0].value;
this.upperCenterY = layer.bounds[1].value;
this.upperRightX = layer.bounds[2].value;
this.upperRightY = layer.bounds[1].value;
this.middleLeftX = layer.bounds[0].value;
this.middleLeftY = this.layerHeight / 2 + layer.bounds[1].value;
this.middleCenterX = this.layerWidth / 2 + layer.bounds[0].value;
this.middleCenterY = this.layerHeight / 2 + layer.bounds[1].value;
this.middleRightX = layer.bounds[2].value;
this.middleRightY = this.layerHeight / 2 + layer.bounds[1].value;
this.lowerLeftX = layer.bounds[0].value;
this.lowerLeftY = layer.bounds[3].value;
this.lowerCenterX = this.layerWidth / 2 + layer.bounds[0].value;
this.lowerCenterY = layer.bounds[3].value;
this.lowerRightX = layer.bounds[2].value;
this.lowerRightY = layer.bounds[3].value;
// I'm adding these for easier editing of flash symbol transformation point (outputs a 'x, y' format)
// because I like to assign shortcut keys that use the numeric pad keyboard, like such:
// 7 8 9
// 4 5 6
// 1 2 3
//
this.leftBottom = this.lowerLeftX + ", " + this.lowerLeftY;
this.bottomCenter = this.lowerCenterX + ", " + this.lowerCenterY;
this.rightBottom = this.lowerRightX + ", " + this.lowerRightY;
this.leftCenter = this.middleLeftX + ", " + this.middleLeftY;
this.center = this.middleCenterX + ", " + this.middleCenterY;
this.rightCenter = this.middleRightX + ", " + this.middleRightY;
this.leftTop = this.upperLeftX + ", " + this.upperLeftY;
this.topCenter = this.upperCenterX + ", " + this.upperCenterY;
this.rightTop = this.upperRightX + ", " + this.upperRightY;
// these return object coordinates relative to layer bounds
this.relUpperLeftX = layer.bounds[1].value - layer.bounds[1].value;
this.relUpperLeftY = layer.bounds[0].value - layer.bounds[0].value;
this.relUpperCenterX = this.layerWidth / 2;
this.relUpperCenterY = layer.bounds[0].value - layer.bounds[0].value;
this.relUpperRightX = this.layerWidth;
this.relUpperRightY = layer.bounds[0].value - layer.bounds[0].value;
this.relMiddleLeftX = layer.bounds[1].value - layer.bounds[1].value;
this.relMiddleLeftY = this.layerHeight / 2;
this.relMiddleCenterX = this.layerWidth / 2;
this.relMiddleCenterY = this.layerHeight / 2;
this.relMiddleRightX = this.layerWidth;
this.relMiddleRightY = this.layerHeight / 2;
this.relLowerLeftX = layer.bounds[1].value - layer.bounds[1].value;
this.relLowerLeftY = this.layerHeight;
this.relLowerCenterX = this.layerWidth / 2;
this.relLowerCenterY = this.layerHeight / 2;
this.relLowerRightY = this.layerHeight;
this.relLowerRightX = this.layerWidth;
this.relLowerRightY = this.layerHeight;
return this;
}
// add header line
//str = "<psd filename=\"" + docRef.name + "\" path=\"" + mySourceFilePath + "\" width=\"" + docWidth + "\" height=\"" + docHeight + "\">\n";
// now a function to collect the data
function exportBounds(doc, layer, i) {
var isVisible = layer.visible;
var layerData = cLayer(doc, layer);
// if(isVisible){
// Layer object main coordinates relative to its active pixels
var str2 = leftTop // this is the
// + "\" layerwidth=\"" + layerData.layerWidth
// + "\" layerheight=\"" + layerData.layerHeight
// + "\" transformpoint=\"" + "center" + "\">" // hard-coding 'center' as the default transformation point
+"\" \"" + layer.name + ".png" + "</layer>\n" // I have to put some content here otherwise sometimes tags are ignored
str += str2.toString();
};
//};
// call X's function using the one above
traverseLayers(app.activeDocument, exportBounds, true);
// Use this to export XML file to same directory where PSD file is located
var mySourceFilePath = activeDocument.fullName.path + "/";
// create a reference to a file for output
var csvFile = new File(mySourceFilePath.toString().match(/([^\.]+)/)[1] + app.activeDocument.name.match(/([^\.]+)/)[1] + ".xls");
// open the file, write the data, then close the file
csvFile.open('w');
csvFile.writeln(str + "</psd>");
csvFile.close();
preferences.rulerUnits = originalRulerUnits;
// Confirm that operation has completed
alert("Operation Complete!" + "\n" + "Layer coordinates were successfully exported to:" + "\n" + "\n" + mySourceFilePath.toString().match(/([^\.]+)/)[1] + app.activeDocument.name.match(/([^\.]+)/)[1] + ".xml");
Change
var str2 = leftTop // this is the
// + "\" layerwidth=\"" + layerData.layerWidth
// + "\" layerheight=\"" + layerData.layerHeight
// + "\" transformpoint=\"" + "center" + "\">" // hard-coding 'center' as the default transformation point
+"\" \"" + layer.name + ".png" + "</layer>\n" // I have to put some content here otherwise sometimes tags are ignored
str += str2.toString();
to
var str2 = leftTop + ","+ layer.name + "\n"
str += str2.toString();
and
var csvFile = new File(mySourceFilePath.toString().match(/([^\.]+)/)[1] + app.activeDocument.name.match(/([^\.]+)/)[1] + ".xls");
to
var csvFile = new File(mySourceFilePath.toString().match(/([^\.]+)/)[1] + app.activeDocument.name.match(/([^\.]+)/)[1] + ".csv");
This works great for me!

Resources