Google Apps Script Dialog Generator
\`)
.setWidth(${dialogWidth})
.setHeight(${dialogHeight});
SpreadsheetApp.getUi().showModelessDialog(html, '${dialogTitle}');
}
// Function to get all items from ${sourceSheet} sheet
function getItems() {
try {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const stockSheet = ss.getSheetByName('${sourceSheet}');
if (!stockSheet) {
throw new Error('${sourceSheet} sheet not found. Please check the sheet name.');
}
const lastRow = stockSheet.getLastRow();
if (lastRow < ${startRow}) {
return [];
}
const itemRange = stockSheet.getRange('${startCol}${startRow}:${endCol}' + lastRow);
const values = itemRange.getValues();
return values.filter(row => row[0] !== '' || row[1] !== '');
} catch (error) {
Logger.log('Error in getItems: ' + error.toString());
throw error;
}
}
${enableSearch ? `
// Function to search items
function searchItems(searchTerm) {
try {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const stockSheet = ss.getSheetByName('${sourceSheet}');
if (!stockSheet) {
throw new Error('${sourceSheet} sheet not found. Please check the sheet name.');
}
const lastRow = stockSheet.getLastRow();
const itemRange = stockSheet.getRange('${startCol}${startRow}:${endCol}' + lastRow);
const values = itemRange.getValues();
return values.filter(row =>
row[0].toString().toLowerCase().includes(searchTerm.toLowerCase()) ||
row[1].toString().toLowerCase().includes(searchTerm.toLowerCase()) ||
row[2].toString().toLowerCase().includes(searchTerm.toLowerCase())
);
} catch (error) {
Logger.log('Error in searchItems: ' + error.toString());
throw error;
}
}` : ''}
// Function to add item to ${destSheet} sheet
function addItemToInvoice(itemCode, itemName, price) {
try {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const invoiceSheet = ss.getSheetByName('${destSheet}');
if (!invoiceSheet) {
throw new Error('${destSheet} sheet not found. Please check the sheet name.');
}
const lastRow = invoiceSheet.getLastRow();
const firstEmptyRow = lastRow < ${destStartRow} ? ${destStartRow} : lastRow + 1;
invoiceSheet.getRange(firstEmptyRow, 1).setValue(itemCode);
invoiceSheet.getRange(firstEmptyRow, 2).setValue(itemName);
invoiceSheet.getRange(firstEmptyRow, 3).setValue(price);
} catch (error) {
Logger.log('Error in addItemToInvoice: ' + error.toString());
throw error;
}
}`;
document.getElementById('codeOutput').textContent = code;
document.getElementById('outputSection').style.display = 'block';
// Generate usage instructions
const instructions = `1. Copy the generated code above
2. Open your Google Sheets spreadsheet
3. Go to Extensions → Apps Script
4. Delete any existing code and paste the generated code
5. Save the project (Ctrl+S)
6. Run the function "${functionName}()" to test
Requirements:
• Source sheet: "${sourceSheet}" with data in columns ${sourceColumns} (starting from row ${startRow})
• Destination sheet: "${destSheet}" (items will be added starting from row ${destStartRow})
Function Name: ${functionName}()`;
document.getElementById('previewContent').textContent = instructions;
// Smooth scroll to output
document.getElementById('outputSection').scrollIntoView({ behavior: 'smooth' });
}
function copyCode() {
const codeOutput = document.getElementById('codeOutput');
const textArea = document.createElement('textarea');
textArea.value = codeOutput.textContent;
document.body.appendChild(textArea);
textArea.select();
document.execCommand('copy');
document.body.removeChild(textArea);
const copyBtn = document.querySelector('.copy-btn');
copyBtn.textContent = '✅ Copied!';
copyBtn.classList.add('success-animation');
setTimeout(() => {
copyBtn.textContent = '📋 Copy Code';
copyBtn.classList.remove('success-animation');
}, 2000);
}
No comments:
Post a Comment