I’m trying to create an application for storing oxygen cylinders, and I’m struggling with implementing code that will transfer values from a sheet named “AUTOMATION OF APPLICATIONS” to a sheet named “Bottle Table”. I would like the columns Location, Date and Time of Entry, Responsible Person, Quantity Issued, Bottle Capacity, and Owner to be transferred 1:1. However, I want the “Barcode” column in the “Bottle Table” sheet to take its values from the “Scanned Barcodes” column in the “AUTOMATION OF APPLICATIONS” sheet, where this column lists all barcode values in one cell, separated by commas. I would like it to take one barcode at a time and create an entry from it.
here’s the code that im using currently i GAS:
function onChange(e) {
var sourceSheetId = “1jhMrEzaD5G_oPY4MtT-A5tm_xtJNRDyFz0Grd2fkKjo”;
var targetSheetId = “1y9f811D0BrZpWpNjpFvWsaa_fSYH-oF0pITP5sDvT1k”;
var sourceSheet = SpreadsheetApp.openById(sourceSheetId);
var targetSheet = SpreadsheetApp.openById(targetSheetId);
// Check if changes are related to the source sheet
var changesRelatedToSourceSheet = e.source.getSheet().getSheetId() === sourceSheet.getSheetId();
if (changesRelatedToSourceSheet) {
// Get data for the newly added row
var newRow = e.range.getRow();
var newDataForRow = sourceSheet.getRange(newRow, 1, 1, sourceSheet.getLastColumn()).getValues()[0];
// Transfer data to the target sheet
var location = newDataForRow[0];
var dateTime = newDataForRow[1];
var person = newDataForRow[2];
var quantityIssued = newDataForRow[3];
var capacity = newDataForRow[4];
var owner = newDataForRow[5];
var barcodeValues = newDataForRow[6].split(', ');
// Transfer data for each barcode
for (var j = 0; j < barcodeValues.length; j++) {
// Find the last row in the target sheet
var lastRow = targetSheet.getLastRow() + 1;
// Transfer data to the target sheet
targetSheet.getRange(lastRow, 1).setValue(location);
targetSheet.getRange(lastRow, 2).setValue(dateTime);
targetSheet.getRange(lastRow, 3).setValue(person);
targetSheet.getRange(lastRow, 4).setValue(quantityIssued);
targetSheet.getRange(lastRow, 5).setValue(capacity);
targetSheet.getRange(lastRow, 6).setValue(owner);
targetSheet.getRange(lastRow, 7).setValue(barcodeValues[j]);
}
}
}
Tried to do looping with actions, but seems im to stupid for that :P.