'Final Element of Array Throwing "TypeError: Cannot read property '0' of undefined"
I'm running the below code in Apps Script. This snippet is supposed to copy the data from several cells from one Google Sheet ('Entry Form') into a new variable called copyData and then paste those same values, one by one, into another sheet ('SCAR Log'). I've determined that this error only occurs when my final array value entryFormData[44][0] is blank. The script only throws this error when the final data cell on my 'Entry Form' is blank.
Strangely, I'm using a nearly exact replica of this code on another sheet that is working perfectly, regardless of whether or not the last array value is blank. Can anyone help shed some light on this?
if (responseOverwrite == uiOverwrite.Button.OK) {
var copyData =
[[entryFormData[4][3], //SCAR No
entryFormData[4][12], //PO No.
entryFormData[8][2], //Date Opened
entryFormData[8][8], //Originator
entryFormData[8][18], //Supplier
entryFormData[10][6], //Supplier Quality Representative
entryFormData[10][18], //SQR Email
entryFormData[13][0], //Drawing No(s). / Part No(s).
entryFormData[13][11], //Serial No(s). / Lot No(s).
entryFormData[13][20], //Job Code(s)
entryFormData[18][0], //Description of Problem
entryFormData[18][13], //Root Cause
entryFormData[23][0], //Immediate Corrective Action
entryFormData[23][13], //Preventative Action Plan
entryFormData[25][9], //Failure Analysis Conducted by Supplier
entryFormData[29][6], //Date of Supplier Response
entryFormData[29][17], //Supplier Action Type
entryFormData[32][0], //Description of Supplier Actions
entryFormData[34][7], //Supplier Response Assessment
entryFormData[34][20], //Assessed by (QA/QM)
entryFormData[37][0], //Assessment Rationale
entryFormData[37][21], //Problem Resolved
entryFormData[41][2], //NCP No(s).
entryFormData[41][9], //RMA No(s).
entryFormData[41][18], //Other
entryFormData[44][0]]]; //Comments and Recommendations
destination.getRange("A" + row + ":Z" + row).setValues(copyData);
break;
}
For clarity, here's the full Apps Script with the rest of the code:
function Save() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var destination = ss.getSheetByName('SCAR Log');
var lastDataRow = destination.getLastRow();
var firstBlankDataRow = destination.getLastRow()+1;
var scarLogData = destination.getDataRange().getDisplayValues();
var uiOverwrite = SpreadsheetApp.getUi();
var uiSaveNew = SpreadsheetApp.getUi();
var entryFormData = ss.getSheetByName('Entry Form').getDataRange().getDisplayValues();
var currentSCARNo = ss.getRange('D5').getDisplayValue();
var filename = "SCAR-" + currentSCARNo;
var currentFormFieldsColor = ss.getRange('C9').getBackground();
var formFieldsArray = ['M5', 'C9', 'S9', 'I9', 'G11', 'S11', 'A14', 'L14', 'U14', 'A19', 'N19', 'A24', 'N24', 'J26', 'G30', 'R30', 'A33', 'H35', 'U35', 'A38', 'V38', 'C42', 'J42', 'S42', 'A45'];
var formFieldsData = ss.getRangeList(formFieldsArray).getRanges().map(range => range.getValues());
// If form field data is not found (but 24 or 25 commas are found)...
if (formFieldsData == ",,,,,,,,,,,,,,,,,,,,,,,," || formFieldsData == ",,,,,,,,,,,,,,,,,,,,,,,,,") {
SpreadsheetApp.getUi().alert("⚠️ Nothing to Save.", "Please complete this SCAR or click \"Clear\" and then select an existing SCAR No. from the \"SCAR No.\" drop-down and press \"Edit\".", SpreadsheetApp.getUi().ButtonSet.OK);
return;
}
//Check if we are in View Mode
if (currentFormFieldsColor == '#b7b7b7') {
SpreadsheetApp.getActive().toast('⚠️ You are currently in View Mode. This data has already been saved.');
return;
}
// Ensure SCAR No. on Entry Form is not blank.
if (entryFormData[4][3] != "") {
// Check column [0] of each SCAR Log row for a matching SCAR No.
for (var i in scarLogData) {
var row = parseInt(i) + 1;
// Matching SCAR No. Found
if (scarLogData[i][0] == entryFormData[4][3]) {
Logger.log("Found a match for \"" + "SCAR NO. " + currentSCARNo + "\" in row #" + row + " on the \"SCAR Log\" sheet");
// Overwrite?
var responseOverwrite = uiOverwrite.alert('⚠️ Overwrite Existing Data?', 'CAR No. ' + currentSCARNo + ' already exists. Would you like to overwrite the log with the data in this form?', uiOverwrite.ButtonSet.OK_CANCEL);
// YES - Overwrite
if (responseOverwrite == uiOverwrite.Button.OK) {
Logger.log('The user clicked "Yes."');
var copyData =
[[entryFormData[4][3], //SCAR No
entryFormData[4][12], //PO No.
entryFormData[8][2], //Date Opened
entryFormData[8][8], //Originator
entryFormData[8][18], //Supplier
entryFormData[10][6], //Supplier Quality Representative
entryFormData[10][18], //SQR Email
entryFormData[13][0], //Drawing No(s). / Part No(s).
entryFormData[13][11], //Serial No(s). / Lot No(s).
entryFormData[13][20], //Job Code(s)
entryFormData[18][0], //Description of Problem
entryFormData[18][13], //Root Cause
entryFormData[23][0], //Immediate Corrective Action
entryFormData[23][13], //Preventative Action Plan
entryFormData[25][9], //Failure Analysis Conducted by Supplier
entryFormData[29][6], //Date of Supplier Response
entryFormData[29][17], //Supplier Action Type
entryFormData[32][0], //Description of Supplier Actions
entryFormData[34][7], //Supplier Response Assessment
entryFormData[34][20], //Assessed by (QA/QM)
entryFormData[37][0], //Assessment Rationale
entryFormData[37][21], //Problem Resolved
entryFormData[41][2], //NCP No(s).
entryFormData[41][9], //RMA No(s).
entryFormData[41][18], //Other
entryFormData[44][0]]]; //Comments and Recommendations
destination.getRange("A" + row + ":Z" + row).setValues(copyData);
ss.toast(entryFormData[44][0]);
break;
}
else if (responseOverwrite == uiOverwrite.Button.CANCEL) {
Logger.log('The user clicked "Cancel" or the close button in the dialog\'s title bar.');
ss.toast("💡 Nothing was saved.");
return;
}
else
ss.toast("💡 Nothing was saved.");
return;
}
// Last Data Row; Matching SCAR No. NOT Found.
else if (row == lastDataRow) {
Logger.log("Did not find a match for \"" + "SCAR NO. " + currentSCARNo + " on the \"SCAR Log\" sheet");
// Save New and Clear.
Logger.log('The user clicked "Yes."');
ss.toast(entryFormData[41][2]);
var copyData = [[entryFormData[4][3], //SCAR No
entryFormData[4][12], //PO No.
entryFormData[8][2], //Date Opened
entryFormData[8][8], //Originator
entryFormData[8][18], //Supplier
entryFormData[10][6], //Supplier Quality Representative
entryFormData[10][18], //SQR Email
entryFormData[13][0], //Drawing No(s). / Part No(s).
entryFormData[13][11], //Serial No(s). / Lot No(s).
entryFormData[13][20], //Job Code(s)
entryFormData[18][0], //Description of Problem
entryFormData[18][13], //Root Cause
entryFormData[23][0], //Immediate Corrective Action
entryFormData[23][13], //Preventative Action Plan
entryFormData[25][9], //Failure Analysis Conducted by Supplier
entryFormData[29][6], //Date of Supplier Response
entryFormData[29][17], //Supplier Action Type
entryFormData[32][0], //Description of Supplier Actions
entryFormData[34][7], //Supplier Response Assessment
entryFormData[34][20], //Assessed by (QA/QM)
entryFormData[37][0], //Assessment Rationale
entryFormData[37][21], //Problem Resolved
entryFormData[41][2], //NCP No(s).
entryFormData[41][9], //RMA No(s).
entryFormData[41][18], //Other
entryFormData[44][0]]]; //Comments and Recommendations
destination.getRange("A" + firstBlankDataRow + ":Z" + firstBlankDataRow).setValues(copyData);
ss.toast("💡 " + filename + " was saved to the Log on row " + firstBlankDataRow + ".");
break;
}
}
// Set the data validation for cell D5 of active sheet to require value from ddSheet A3:A, with dropdown menu.
var ddSheet = ss.getSheetByName('Entry Form.Drop Downs (HS)');
var ddCell = ss.getRange('D5');
var ddRange = ddSheet.getRange('A3:A');
var ddRule = SpreadsheetApp.newDataValidation().requireValueInRange(ddRange, true).build();
// define the total Entry Form range for setValues
var destinationEntryForm = ss.getSheetByName('Entry Form');
var efRange = destinationEntryForm.getRange(1,1,45,23);
// get the array values
var efValues = efRange.getValues();
// adjust some array values
efValues[4][12] = '=iferror(vlookup(D5,\'SCAR Log\'!A:B,2,0),"")'; //PO No.
efValues[8][2] = '=iferror(vlookup(D5,\'SCAR Log\'!A:C,3,0),"")'; //Date Opened
efValues[8][8] = '=iferror(vlookup(D5,\'SCAR Log\'!A:D,4,0),"")'; //Originator
efValues[8][18] = '=iferror(vlookup(D5,\'SCAR Log\'!A:E,5,0),"")'; //Supplier
efValues[10][6] = '=iferror(vlookup(D5,\'SCAR Log\'!A:F,6,0),"")'; //Supplier Quality Representative
efValues[10][18] = '=iferror(vlookup(D5,\'SCAR Log\'!A:G,7,0),"")'; //SQR Email
efValues[13][0] = '=iferror(vlookup(D5,\'SCAR Log\'!A:H,8,0),"")'; //Drawing No(s). / Part No(s).
efValues[13][11] = '=iferror(vlookup(D5,\'SCAR Log\'!A:I,9,0),"")'; //Serial No(s). / Lot No(s).
efValues[13][20] = '=iferror(vlookup(D5,\'SCAR Log\'!A:J,10,0),"")'; //Job Code(s)
efValues[18][0] = '=iferror(vlookup(D5,\'SCAR Log\'!A:K,11,0),"")'; //Description of Problem
efValues[18][13] = '=iferror(vlookup(D5,\'SCAR Log\'!A:L,12,0),"")'; //Root Cause
efValues[23][0] = '=iferror(vlookup(D5,\'SCAR Log\'!A:M,13,0),"")'; //Immediate Corrective Action
efValues[23][13] = '=iferror(vlookup(D5,\'SCAR Log\'!A:N,13,0),"")'; //Preventative Action Plan
efValues[25][9] = '=iferror(vlookup(D5,\'SCAR Log\'!A:O,15,0),"")'; //Failure Analysis Conducted by Supplier
efValues[29][6] = '=iferror(vlookup(D5,\'SCAR Log\'!A:P,16,0),"")'; //Date of Supplier Response
efValues[29][17] = '=iferror(vlookup(D5,\'SCAR Log\'!A:Q,17,0),"")'; //Supplier Action Type
efValues[32][0] = '=iferror(vlookup(D5,\'SCAR Log\'!A:R,18,0),"")'; //Description of Supplier Actions
efValues[34][7] = '=iferror(vlookup(D5,\'SCAR Log\'!A:S,19,0),"")'; //Supplier Response Assessment
efValues[34][20] = '=iferror(vlookup(D5,\'SCAR Log\'!A:T,20,0),"")'; //Assessed by (QA/QM)
efValues[37][0] = '=iferror(vlookup(D5,\'SCAR Log\'!A:U,21,0),"")'; //Assessment Rationale
efValues[37][21] = '=iferror(vlookup(D5,\'SCAR Log\'!A:V,22,0),"")'; //Problem Resolved
efValues[41][2] = '=iferror(vlookup(D5,\'SCAR Log\'!A:W,23,0),"")'; //NCP No(s).
efValues[41][9] = '=iferror(vlookup(D5,\'SCAR Log\'!A:X,24,0),"")'; //RMA No(s).
efValues[41][18] = '=iferror(vlookup(D5,\'SCAR Log\'!A:Y,25,0),"")'; //Other
efValues[44][0] = '=iferror(vlookup(D5,\'SCAR Log\'!A:Z,26,0),"")'; //Comments and Recommendations
// update the range for the adjusted array values
efRange.setValues(efValues);
//Show Drop Down Arrow on Cell D5
ddCell.setDataValidation(ddRule);
//Set background color to gray
ss.getRangeList(['M5', 'C9', 'S9', 'I9', 'G11', 'S11', 'A14', 'L14', 'U14', 'A19', 'N19', 'A24', 'N24', 'J26', 'G30', 'R30', 'A33', 'H35', 'U35', 'A38', 'V38', 'C42', 'J42', 'S42', 'A45']).setBackground('#b7b7b7');
}
else {
// Display a dialog box with a message and "Yes" and "No" buttons.
SpreadsheetApp.getUi().alert("⚠️ Nothing to Save", "Please select a SCAR No. from the \"SCAR No.\" drop-down and press \"Edit\" or click \"New\" to begin a new report.", SpreadsheetApp.getUi().ButtonSet.OK);
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
