Skip to content

Instantly share code, notes, and snippets.

@manuwell
Created April 15, 2022 17:42
Show Gist options
  • Save manuwell/12e53cedc24f8f5cb51589f6bb6a0414 to your computer and use it in GitHub Desktop.
Save manuwell/12e53cedc24f8f5cb51589f6bb6a0414 to your computer and use it in GitHub Desktop.
Script AppsScript Sync ONG Adote Um Gatinho (AUG)
// planilhas de teste:
// https://drive.google.com/drive/folders/19bJHxbbAcbky8dO0DB3-T2bmwtJCzc8P
// para que esta função funcione vc. precisa:
// . nomear o intervalo da coluna que guardará o status do sync com o nome "financeiro"
// . criar uma aba chamada config-sync com 2 colunas: Config | Valor
// . configurar as propriedades:
// . URL Planilha Financeiro
// . Aba Planilha Financeiro
// . Cebeçalho (usar formula join para concatenar o cabecalho separado por ';')
function sendToDestino() {
let ss = SpreadsheetApp.getActiveSheet();
let configSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('config-sync')
let sheetUrl = _getConfig(configSheet, 'URL Planilha Financeiro')
let sheetName = _getConfig(configSheet, 'Aba Planilha Financeiro')
if(sheetUrl == null || sheetName == null) {
SpreadsheetApp.getUi().alert('Erro ao buscar a planilha financeiro!')
return;
}
Logger.info(sheetUrl)
let header = _getConfig(configSheet, 'Cabeçalho')
let wasSentHeader = false
let range = SpreadsheetApp.getActiveSpreadsheet().getRangeByName('financeiro');
let columnFinanceiroIndex = range.getColumn()
let fianceiroData = range.getValues()
fianceiroData.forEach((row,i) => {
if(row[0] != "pendente"){ return; }
if(header != null && !wasSentHeader){
wasSentHeader = true
_syncToFinanceiro(sheetUrl, sheetName, header.split(";"))
}
let rowRange = ss.getRange(i+1, 1, 1, 40)
let rowValues = rowRange.getValues()
_syncToFinanceiro(sheetUrl, sheetName, rowValues[0])
let columnFinanceiro = ss.getRange(i+1, columnFinanceiroIndex)
columnFinanceiro.setValue("enviado")
})
}
function _syncToFinanceiro(sheetUrl, sheetName, dataRow){
destinoSS = SpreadsheetApp.openByUrl(sheetUrl)
destinoSheet = destinoSS.getSheetByName(sheetName)
destinoSheet.appendRow(dataRow)
}
function _getConfig(configSheet, name){
let configRange = configSheet.getRange(1,1,100,2).getValues();
let config = configRange.find(row => {
return row[0] == name
});
if(config == null) { return null }
return config[1];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment