function createCoc(sheetId, templateId, folderUrl, jobId, inspectorEmail, sampleDate, customer, address, lab, inspectorName, turnaroundTime) {
console.log("Received lab value:", lab);
console.log("Received tat value:", turnaroundTime);
// Get the spreadsheet and template
const spreadsheet = SpreadsheetApp.openById(sheetId);
const template = DriveApp.getFileById(templateId);
// Extract TAT using regular expression
const regex = /-(.*)/;
const match = regex.exec(turnaroundTime);
const extractedTAT = match ? match[1] : "";
// Get specific sheets
const usersSheet = spreadsheet.getSheetByName('Users');
const labsSheet = spreadsheet.getSheetByName('Labs');
const samplesSheet = spreadsheet.getSheetByName('Samples');
// Get all data in one go for efficiency
const usersData = usersSheet.getDataRange().getValues();
const labsData = labsSheet.getDataRange().getValues();
const samplesData = samplesSheet.getDataRange().getValues();
// Find the row with the specified job ID
const dataRange = spreadsheet.getDataRange();
const data = dataRange.getValues();
const row = data.find(row => row[0] === jobId);
// Find the inspector row
const inspectorRow = usersData.find(row => row[0] === inspectorEmail);
const inspectorPhone = inspectorRow[3];
// Find the lab row
const labName = lab[0];
const labRow = labsData.find(row => row[1] === labName);
const labAddress = labRow[5];
const labPhone = labRow[6];
const labEmail = labRow[7];
const labCustid = labRow[8];
// Extract folder ID from the URL
const folderIdRegex = /[-\w]{25,}/;
const folderIdMatch = folderUrl.match(folderIdRegex);
const destinationFolderId = folderIdMatch[0];
// Create a copy of the template
const copy = template.makeCopy(`Job-${row[1]} COC`, DriveApp.getFolderById(destinationFolderId));
// Open the copy and replace placeholders with data
const doc = DocumentApp.openById(copy.getId());
const body = doc.getBody();
const footer = doc.getFooter();
const header = doc.getHeader();
// Replace placeholders (adjust indices as needed)
if (customer !== undefined && customer !== null) {
body.replaceText('{{Customer}}', customer);
} else {
body.replaceText('{{Customer}}', '');
}
body.replaceText('{{InspectorEmail}}', inspectorEmail);
body.replaceText('{{Address}}', address);
body.replaceText('{{SamplingDate}}', sampleDate);
body.replaceText('{{Inspector}}', inspectorName);
body.replaceText('{{InspectorPhone}}', inspectorPhone);
body.replaceText('{{TAT_Selected}}', extractedTAT);
body.replaceText('{{Lab}}', lab);
body.replaceText('{{LabAddress}}', labAddress);
body.replaceText('{{LabPhone}}', labPhone);
body.replaceText('{{LabEmail}}', labEmail);
body.replaceText('{{LabCustid}}', labCustid ? labCustid : 'N/A');
//body.replaceText('{{LabCustid}}', labCustid);
// Find the corresponding samples for this job ID
const samplesForJob = samplesData.filter(sampleRow => sampleRow[0] === jobId);
// Filter samples with non-zero sample numbers
const filteredSamples = samplesForJob.filter(sampleRow => sampleRow[2] !== 0);
// Find the maximum sample number for the job ID
const maxSampleNumber = samplesForJob.reduce((max, sample) => Math.max(max, sample[2]), 0);
// Replace the placeholder in the document
body.replaceText('{{#ofSamples}}', maxSampleNumber.toString());
// Find the table in the document
let table = null;
for (let i = 0; i < body.getNumChildren(); i++) {
const child = body.getChild(i);
if (child.getType() === DocumentApp.ElementType.TABLE) {
const headerRow = child.getRow(0);
if (headerRow.getCell(0).getText() === 'Sample #') { // Replace with your actual header text
table = child;
break;
}
}
}
if (!table) {
// Handle the case where the table is not found
Logger.log('Table not found');
return;
}
// Populate the table with sample data
const numRows = table.getNumRows();
const numSamples = samplesForJob.length;
// Ensure there are enough rows in the table
for (let i = numRows; i < numSamples; i++) {
table.appendTableRow();
}
// Populate the table with sample data
//samplesForJob.forEach((sampleRow, index) => {
filteredSamples.forEach((sampleRow, index) => {
const row = table.getRow(index + 1); // Start from the second row
row.getCell(0).setText(sampleRow[2]); // Assuming sample ID is in column C
row.getCell(1).setText(sampleRow[3]); // Assuming sample description is in column D
});
// Delete extra rows from the bottom
let i = filteredSamples.length + 1; // Start from the row after the last populated row
while (i < table.getNumRows()) {
table.deleteRow(i);
}
doc.saveAndClose();
}
the error I get on this one is:
TypeError: table.deleteRow is not a function at createCoc(Code:126:9)
I get similar errors for other attempts such as "row.remove".
How can I have the table resize dynamically based on the number of rows of sample data needed from the specific "job"? I am brand spankin' new at this so please be gentle! 🙂 Any help is much appreciated!