I have script that works fine as editor add-on but when I am testing as workspace add-on (as a stand alone appscript) the functions takes too long to run, it does what actually planned, but gives error at the end (run time error).
Planning to keep record of made invoice. i would like to get the range values from Invoicing sheet and paste it as a row without a gap between cells to last available row into Broker Invoice and get new invoice number. this script works only when you are in specific sheet to avoid unintended copies. My question is : does always google add-ons works to slow or there is another way to do this function logics, and how to get rid of the run time error?
function saveRecord() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const formWS = ss.getSheetByName("Invoicing");
const dataWS = ss.getSheetByName("Broker invoice");
// Check if the active sheet is "Invoicing"
if (ss.getActiveSheet().getName() !== "Invoicing") {
SpreadsheetApp.getUi().alert('Please switch to the "Invoicing" sheet to run the function.');
return;
}
// Get the current ID from H11 and increment it for the next use
const idCell = formWS.getRange("H11");
const currentID = idCell.getValue();
const nextID = currentID + 1;
// Define the ranges to copy and batch get values
const ranges = ["N11", "H12", "H14", "E2:H2", "B11:E11", "H15", "B22:C22", "D22:E22", "E27:H28"];
const fieldValues = [currentID];
// Collect all values and flatten them
ranges.forEach(range => {
fieldValues.push(...formWS.getRange(range).getValues().flat().filter(value => value !== ''));
});
// Append the combined values to the "Broker invoice" sheet
dataWS.appendRow(fieldValues);
// Update the ID cell with the next ID
idCell.setValue(nextID);
}```
how to get the function to run faster and without runtime error