@
Michael O'Shaughnessy and @Keith Andersen
The sample code you provided was helpful, but I am confused how to structure it. The example uses data that already exists in a spreadsheet. I am using order numbers stored in an empty array and then batch process that array using the last order id processed. The hope is to write the data all at once when the script completes processing. Maybe I should write to the sheet in batches?
This is what I am working with so far;
function processOrdersBatch(orderIds) {
const startTime = new Date().getTime();
const maxExecutionTime = 280000; // leaving some margin from 6-minute limit
// Retrieve the last processed order ID, if any
let lastProcessedOrderId = getLastProcessedOrderId();
let startIndex = lastProcessedOrderId ? orderIds.indexOf(lastProcessedOrderId) + 1 : 0;
for (let i = startIndex; i < orderIds.length; i++) {
const orderId = orderIds[i];
// Process the order ID (e.g., fetch transactions, calculate totals, etc.)
processOrder(orderId);
// Check if approaching the execution time limit
if (new Date().getTime() - startTime > maxExecutionTime) {
console.log("Approaching max execution time, stopping...");
setLastProcessedOrderId(orderId);
createTriggerIfNeeded(); // Correctly adjusted to target a wrapper function if needed
return; // Exit function to ensure trigger creation logic is not bypassed
}
}
// Delete the trigger if all orders have been processed
deleteTriggerIfNeeded();
}
function setLastProcessedOrderId(orderId) {
const scriptProperties = PropertiesService.getScriptProperties();
scriptProperties.setProperty('lastProcessedOrderId', orderId.toString());
}
I don't know if this is the most effective and efficient way to reduce script time runs...the was successfully writing each row to the sheet one at a time, but ran into an error when the trigger ran the second time.
try {
let response = UrlFetchApp.fetch(transactionsUrl, params);
// console.error(`Error fetching transactions: ${error} Response: ${response.getContentText()}`);
if (response.getResponseCode() === 200) {
console.log(`Successfully fetched transactions for order ID: ${orderId}`);
const transactionsData = JSON.parse(response.getContentText()).transactions;
transactionsData.forEach(txn => {
let data = [txn.order_id, txn.id, txn.kind, txn.gateway, txn.status, txn.message, txn.created_at, txn.processed_at, txn.amount];
allTransactionsData.push(data);
});
// Update the last processed order ID
setLastProcessedOrderId(orderId);
} else {
console.error(`Failed to fetch transactions for order ID ${orderId}: ${response.getResponseCode()}`);
}
} catch (error) {
console.error(`Error fetching transactions for order ID ${orderId}:`, error.toString());
}
console.log("All transactions have been fetched. Now writing to sheet...");
if (allTransactionsData.length > 0) {
appendTransactionsToSheet2(allTransactionsData, spreadsheetId, transactionsSheetName);
console.log("Data writing complete. Process finished.");
}
I hope that makes sense in what I am trying to do. Thanks for your help.