Skip to content

Instantly share code, notes, and snippets.

@mattiaferigutti
Last active December 3, 2022 20:02
Show Gist options
  • Save mattiaferigutti/2ad7fc20e04d17610b23c609f57ed1f4 to your computer and use it in GitHub Desktop.
Save mattiaferigutti/2ad7fc20e04d17610b23c609f57ed1f4 to your computer and use it in GitHub Desktop.
function mergeIntoMainSheet() {
let spreadSheet = SpreadsheetApp.getActiveSpreadsheet()
/**
* Inserisci il nome della tabella dove vuoi importare tutti i dati
*/
let mainSheet = spreadSheet.getSheetByName('Tabella Totale')
/**
* Inserisci tutti i nomi dei clienti da cui vuoi copiare i dati e inserirli nella tabella principale definita sopra
*/
let clientsSheet = ['Alessandra Gatti', 'Mattia Pasquetto']
for(let i = 0; i < clientsSheet.length; i++) {
let client = clientsSheet[i]
let clientSheet = spreadSheet.getSheetByName(client)
copyDataIntoMainSheet(i, mainSheet, clientSheet, client)
}
}
function copyDataIntoMainSheet(i, mainSheet, clientSheet, clientName) {
var margin = 0
if (i > 0) margin = 4
let availableRow = getBusyRowsNumber(mainSheet)
let clientRange = getDataRangeFromClientSheet(clientSheet)
mainSheet.getRange(availableRow + margin, 1).setValue(clientName.toUpperCase())
mainSheet.getRange(availableRow + margin + 1, 1, clientRange.getNumRows(), clientRange.getNumColumns()).setValues(clientRange.getValues())
}
function getDataRangeFromClientSheet(clientSheet) {
return clientSheet.getRange(2, 1, getBusyRowsNumber(clientSheet), getBusyColumnsNumber(clientSheet)) // row, cloumn
}
function getBusyRowsNumber(sheet) {
return sheet.getDataRange().getLastRow()
}
function getBusyColumnsNumber(sheet) {
return sheet.getDataRange().getLastColumn()
}
/**
* Only for test purposes
*/
function testBusyMethod() {
let spreadSheet = SpreadsheetApp.getActiveSpreadsheet()
let sheet = spreadSheet.getSheetByName('Alessandra Gatti')
console.log(getBusyColumnsNumber(sheet))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment