Pages

Se afișează postările cu eticheta GAscript. Afișați toate postările
Se afișează postările cu eticheta GAscript. Afișați toate postările

sâmbătă, 2 decembrie 2023

News : Chart with sizes values from Google Drive.

Visual data representation in graphical format can solve many of today's issues.
Here is a tutorial with a Google Apps Script script that allows you to view the size of the files in Google Drive in Chart Pie format and modify it.
You can find more tutorials about Google Apps Script on my Google site.
Let's see the source code:
function generateDriveUsageReportPie() {
  var drive = DriveApp.getRootFolder();
  var files = drive.getFiles();

  var data = [['Nume Fișier', 'Dimensiune (KB)']];
  
  while (files.hasNext()) {
    var file = files.next();
    var fileSizeBytes = file.getSize();
    var fileSizeKB = fileSizeBytes / 1024; // Convertim dimensiunea în KB
    data.push([file.getName(), fileSizeKB]);
  }

  // open Google Sheet
  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = spreadsheet.getSheetByName('Drive Usage Report');
  if (!sheet) {
    sheet = spreadsheet.insertSheet('Drive Usage Report');
  }

  // clean sheet
  sheet.clear();

  // write values
  sheet.getRange(1, 1, data.length, data[0].length).setValues(data);

  // clean charts from Google Sheets
  var charts = sheet.getCharts();
  for (var i = 0; i < charts.length; i++) {
    sheet.removeChart(charts[i]);
  }

  // create and add the Pie Chart with all values
  var chart = sheet.newChart()
    .asPieChart()
    .setTitle('Utilizare Google Drive')
    .addRange(sheet.getRange(2, 1, data.length - 1, 2))  // Exclude header row
    .setPosition(5, 1, 0, 0)
    .setOption('title', 'Utilizare Google Drive')
    .setOption('legend', {position: 'top'})
    .build();

  sheet.insertChart(chart);

  Logger.log('Raport generat cu succes.');
}
Here is the result obtained: