Home > Enterprise >  How to make an ascendant sort of the data for the column B and keep the associated values for the de
How to make an ascendant sort of the data for the column B and keep the associated values for the de

Time:04-10

Good morning to the community.

I'm working on Google Sheets. I'm using Apps Script. You can find my file in the following link.

  • [https://docs.google.com/spreadsheets/d/1ce5sppfkftIh0uDsODDmXb_9Cd-hfh5w_q3lIoIS5o8/edit?usp=sharing]

I have one sheet named "journal" where I have the data of my journal.

Journal
Année 2022
Mois 3
Date Compte Détail des opérations Débit Crédit
06-mars 6 Groover 24,00
06-mars 512 Banque 24,00
facture fournisseur n°
12-mars 7 Bus balladium concert 135,00
12-mars 53 Caisse 135,00
versement client n°
28-mars 6 Groover 24,00
28-mars 512 Banque 24,00
facture fournisseur n°
Totaux à reporter 183,00 183,00

I have also one sheet named "grand livre" where I tried to report the data of the sheet "journal" and sort the data of the accounts (column B) in an ascendant way. But, the line 9 with the account "7" is not at the end and should be at the end.

Débit Crédit
06-mars 512 24
28-mars 512 24
12-mars 53 135
12-mars 7 135
06-mars 6 24
28-mars 6 24
à reporter 183 183
function monGrandLivre() {
  var journal = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('journal')
  var data = journal.getDataRange().getDisplayValues().filter(r => (!isNaN(Date.parse(r[0]))))
  data = data.sort(function (a, b) {
    return b[1] - a[1];
  });
  var result = []
  result.push(['', '', '', 'Débit', 'Crédit'])
  data.forEach(function (donnees, ligne) {
    result.push([donnees[0], donnees[1], '', donnees[3], donnees[4]])
    if (ligne != (data.length - 1)) {
      if (data[ligne   1][1] != data[ligne][1]) {
        result.push(['', '', '', '', ''])
        result.push(['', '', '', '', ''])
      }
    }
  })
  var grandLivre = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('grand livre')
  grandLivre.clearContents()
  grandLivre.getRange(1, 1, result.length, result[0].length).setValues(result)
  grandLivre.getRange(result.length   1, 3).setValue('à reporter')
  grandLivre.getRange(result.length   1, 4).setFormula('=sum(D2:D'   result.length   ')')
  grandLivre.getRange(result.length   1, 5).setFormula('=sum(E2:E'   result.length   ')')
}

Can you help me?

Thank you in advance.

CodePudding user response:

If you want to sort column B by text and not by value, change as follows

  data = data.sort(function (a, b) {
    return (''   a[1]).localeCompare(b[1]);
  })
  • Related