function ebayMacro() {
SpreadsheetApp.getActiveSpreadsheet().setSpreadsheetLocale("de")
const ss = SpreadsheetApp.getActiveSheet()
let lr = ss.getLastRow()
ss.getRange(1,1,lr,1).splitTextToColumns(SpreadsheetApp.TextToColumnsDelimiter.SEMICOLON)
ss.createTextFinder("--").replaceAllWith("")

//Delete first rows (Not part of the transactions)
lr = ss.getLastRow()
let firstArray = ss.getRange(1,1,lr,1).getValues()
let first = firstArray.flat()
let firstTransaction = first.indexOf("Enddatum",0)
ss.deleteRows(1,firstTransaction+1)
lr = lr - (firstTransaction-1)

ss.getRange(1,1,lr+2,1).setNumberFormat("dd.MM.yyyy")

//Determine Last row, Last column, Top Array, First column array

let lc = ss.getLastColumn()
ss.insertColumnsAfter(lc,2)
let topRange = ss.getRange(1,1,1,lc)
let topArrayBD = topRange.getValues()
let topArray = topArrayBD.flat()



//Add new column witht sum of all fees (Columns before "Transaktionsbetrag (inkl. Kosten)")
const iFee = topArray.indexOf("Transaktionsbetrag (inkl. Kosten)") +1
    ss.insertColumnBefore(iFee)
    lc=ss.getLastColumn()
    lr=ss.getLastRow()
    let totalFees = ss.getRange(2,iFee-5,lr-1,5).getValues()
    let sum = 0
    let finalArray = []
    for (let i = 0; i<totalFees.length; i++) {
      for (let j = 0; j<totalFees[i].length; j++) {
        if (typeof totalFees[i][j] == "number") {
        sum += totalFees[i][j]
        }
      }
      finalArray.push(sum)       
      sum = 0
    } 
  let suma = 0
    for (let i of finalArray) {
      suma += i
    }
  let finalArrayBD = finalArray.map( x => x = [x] )
  ss.getRange(1,iFee,1,1).setValue("summe")
  ss.getRange(2,iFee,lr-1,1).setValues(finalArrayBD)


//Add new line of data at the bottom
topRange = ss.getRange(1,1,1,lc)
topArrayBD = topRange.getValues()
topArray = topArrayBD.flat()
const iTyp = topArray.indexOf("Typ") +1
const iBetragAbz = topArray.indexOf("Betrag abzügl. Kosten") +1
const itransdatum = topArray.indexOf("Transaktionsdatum") +1
const iNameKaufer = topArray.indexOf("Name des Käufers") +1
const iTransBetrag = topArray.indexOf("Transaktionsbetrag (inkl. Kosten)") +1
const iTransWarung = topArray.indexOf("Transaktionswährung") +1
const iBestell = topArray.indexOf("Alte Bestellnummer") +1
const iBestellNummer = topArray.indexOf("Bestellnummer") +1
const iAuzahlungsNum = topArray.indexOf("Auszahlungsnr.") +1

ss.getRange(lr+1,itransdatum,1,1).setValue(ss.getRange(2,itransdatum,1,1).getValue())
ss.getRange(lr+1,iNameKaufer,1,1).setValue("eBay Verkaufsgebühren")
ss.getRange(lr+1,iTransBetrag,1,1).setValue(suma)
ss.getRange(lr+1,iTransWarung,1,1).setValue("EUR")
ss.getRange(lr+1,iBestell,1,1).setValue("Verkaufsgebühren")

const arrBetAbzBD = ss.getRange(2,iBetragAbz,lr-1,1).getValues()
const arrBetAbz = arrBetAbzBD.flat()

const arrTypBD = ss.getRange(2,iTyp,lr-1,1).getValues()
const arrTyp = arrTypBD.flat()

let sumaTyp = 0
for (let i = 0; i<arrTyp.length;i++) {
  if (arrTyp[i] == "Andere Gebühr") {
      sumaTyp += arrBetAbz[i]
  }

}
ss.getRange(lr+2,iTransBetrag,1,1).setValue(sumaTyp)
ss.getRange(lr+2,itransdatum,1,1).setValue(ss.getRange(2,itransdatum,1,1).getValue())
ss.getRange(lr+2,iNameKaufer,1,1).setValue("eBay Angebotsgebühren")
ss.getRange(lr+2,iTransWarung,1,1).setValue("EUR")
ss.getRange(lr+2,iBestell,1,1).setValue("Angebotsgebühren")

ss.getRange(1,iTransBetrag,1,1).setValue("Betrag")
ss.getRange(1,itransdatum,1,1).setValue("Buchungsdatum")
ss.getRange(1,iTyp,1,1).setValue("Auftragsart")////////////////////------------------------------
ss.getRange(1,iNameKaufer,1,1).setValue("Empfänger/Zahlungspflichtiger")
ss.getRange(1,iTransWarung,1,1).setValue("Währung")
ss.getRange(1,iBestell,1,1).setValue("Buchungstext")
ss.getRange(1,iAuzahlungsNum,1,1).setValue("Verwendungszweck")
ss.getRange(1,iBestellNummer,1,1).setValue("Zahlungsreferenz")

// Mark columns to be filtered
    topRange = ss.getRange(1,1,1,lc+1)
    topArrayBD = topRange.getValues()
    topArray = topArrayBD.flat()
    const stayArray = ["Buchungsdatum","Buchungstext","Zahlungsreferenz","Verwendungszweck","Betrag","Empfänger/Zahlungspflichtiger","Währung","Auftragsart"]

    //Filter irrelevant Columns 
    const indices = [];
    topArray.forEach((element, index) => {
      if(!stayArray.includes(element)){
         indices.push(index);
      };
   });

    //Delete irrelevant columns
    for (let i =indices.length-1; i >=0; i-- ) {
        ss.deleteColumn(indices[i]+1)
      }


    //Delete possible empty rows
    let rows = ss.getDataRange();
    let numRows = rows.getNumRows();
    let values = rows.getValues();

    let rowsDeleted = 0;
    for (var i = 0; i <= numRows - 1; i++) {
    let row = values[i];
    if (row[6] == '') {
          ss.deleteRow((parseInt(i)+1) - rowsDeleted);
          rowsDeleted++;
        }
      }
  ss.getRange("G2:G"+(lr+3)).setNumberFormat("0.00").createTextFinder(",").replaceAllWith(".")
  ss.getRange("E2:E"+(lr+3)).createTextFinder(",").replaceAllWith("")
  ss.getRange("G2:G"+(lr+3)).setHorizontalAlignment("right")
  ss.deleteColumn(stayArray.length+1)

}
