I got the code below, which is not plotting the values as it should.
The idea is to pivot it, in a way that for each date in row 4, we have the set of products, quantities and other information repeated so as to create a DB like table.
function salvarPrevProducao() {
const srcSheetName = "Previsão Entreposto"; // This is the source sheet name.
const dstSheetName = "DB"; // Please set the destination sheet name.
// This is from https://stackoverflow.com/a/44563639
Object.prototype.get1stNonEmptyRowFromBottom = function (columnNumber, offsetRow = 1) {
const search = this.getRange(offsetRow, columnNumber, this.getMaxRows()).createTextFinder(".").useRegularExpression(true).findPrevious();
return search ? search.getRow() : offsetRow;
};
// 1. Retrieve values from the source sheet.
const ss = SpreadsheetApp.getActiveSpreadsheet();
const srcSheet = ss.getSheetByName(srcSheetName);
const lastRow = srcSheet.get1stNonEmptyRowFromBottom(1);
const [[, , , ...header1], header2, ...srcValues] = srcSheet.getRange("A4:M" + lastRow).getValues();
Logger.log('Header1: ' + header1)
Logger.log('Header2: ' + header2)
// 2. Create an array for putting to the destination sheet.
const values = header1.reduce((ar, h, i) => {
srcValues.forEach(([a, b, c, ...dm]) => ar.push([h, a, b, c, dm[i] || 0, "", dm.pop(), h]));
return ar;
}, [["Data", "Tipo", "Cod", "Descrição", "Qtd", "Usuário", "TimeStamp", "Semana"]]);
// 3. Put the array to the destination sheet.
const dstSheet = ss.getSheetByName(dstSheetName);
dstSheet.getRange(dstSheet.getLastRow() + 1, 1, values.length, values[0].length).setValues(values);
}
Here's what the data looks like prior to processing:
Here's how it should look as the result:
Here's the link to the spreadsheet: https://docs.google.com/spreadsheets/d/13WmD19BkkM-lpvMhu06SbfkPF-KSdmU9ZltAKc2mxmc/edit#gid=240927939
It's doing it right until the column where we have a date informed. For Usuário and Timestamp, it apparently repeats it all at the bottom and these 2 columns don't come in the first chunck of data.
Appreciate any help, as I can't even find where to begin.
Regards