Kodas

 avatar
unknown
plain_text
2 years ago
4.8 kB
4
Indexable
function createFolder() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var year = sheet.getRange("B1").getValue();
  var month = sheet.getRange("B2").getValue();
  var parentFolder = DriveApp.getRootFolder();
  var folderName = year + " " + month + " Mokiniu Saskaitos";
  var existingFolder = parentFolder.getFoldersByName(folderName);

  if (existingFolder.hasNext()) {
    return existingFolder.next();
  } else {
    return parentFolder.createFolder(folderName);
  }
}

function generateInvoices() {
  // Create the folder based on year and month
  var folder = createFolder();

  var sheet = SpreadsheetApp.getActiveSheet();
  var year = sheet.getRange("B1").getValue();
  var month = sheet.getRange("B2").getValue();
  var data = sheet.getRange("B3:G" + sheet.getLastRow()).getValues();

  // Loop through the data and generate invoices
  for (var i = 0; i < data.length; i++) {
    var name = data[i][0];
    var email = data[i][1];
    var received = parseFloat(data[i][2]).toFixed(2) + "€"; // Format as a number with 2 decimal places and add Euro sign
    var spent = parseFloat(data[i][3]).toFixed(2) + "€";   // Format as a number with 2 decimal places and add Euro sign
    var amount = parseFloat(data[i][4]).toFixed(2) + "€";  // Format as a number with 2 decimal places and add Euro sign
    var compensation = parseFloat(data[i][5]).toFixed(2) + "€";  // Format as a number with 2 decimal places and add Euro sign

    // Create a Google Document as the invoice
    var doc = DocumentApp.create("Invoice for " + name + " - " + month);
    var body = doc.getBody();

    // Add static information to the invoice
    body.appendParagraph("SĄSKAITA");

    // Create a 1x2 table for the first cell on the left and second cell on the right
    var table = [];
    var firstRow = [];

    // First cell on the left
    var leftCellText = "UAB AMBER CATERING\n" +
      "Dūmų 3, Vilnius\n" +
      "Tel.868545888\n" +
      "El. paštas: ac.ritulia@gmail.com";

    firstRow.push(leftCellText);

    // Second cell on the right
    var rightCellText = "Rekvizitai mokantiems pavedimu:\n" +
      "Įmonės kodas .. 120857076\n" +
      "PVM kodas ...... LT208570716\n" +
      "sąskaita LT147300010155984049\n" +
      "„Swedbank“, AB\n" +
      "banko kodas 73000\n" +
      "Paskirtyje nurodykite:\n" +
      "mokinio vardą, pavardę, klasę";

    firstRow.push(rightCellText);

    // Add the 1x2 table to the document
    table.push(firstRow);
    body.appendTable(table);

    // Continue with the rest of your dynamic information and tables as before
    body.appendParagraph("Už " + year + " m. " + month + " mėnesio maitinimą\nmokinys(ė): " + name);
    body.appendParagraph(""); // One row gap

    // Add the table
    var table = [];
    table.push(["Paslaugos pavadinimas", "Suma Eur"]);
    table.push(["Gauta suma – apmokėta už suteiktas paslaugas", received]);
    table.push(["Sunaudota - suvalgyta per ataskaitinį laikotarpį", spent]);
    table.push(["Likutis*", amount]);
    table.push(["", "* Suma su minuso ženklu reiškia trūkumą lėšų „vaiko sąskaitoj“"]);
    body.appendTable(table);

    // Add spacing and additional information
    body.appendParagraph("");
    body.appendParagraph("PAPILDOMA INFORMACIJA:");
    body.appendParagraph("Savivaldybės kompensacija yra įtraukta į mokėtiną sumą.");
    body.appendParagraph("Mokėjimo grafoje „Likutis“  su minuso ženklu reiškia trūkumą lėšų vaiko sąskaitoje");
    body.appendParagraph("Už vaikų maitinimą mokama į priekį. Už einamąjį mėnesį mokama per pirmą einamojo mėnesio savaitę. Reikia susiskaičiuoti patiems kiek planuojate valgyti. Jei per einamąjį mėnesį neišnaudojote įmokėtų pinigėlių, permoka perkeliama į sekantį mėnesį.");
    body.appendParagraph("Pietų kaina...................................... 5,40");
    body.appendParagraph("Pavakarių kaina............................. 2,30");
    body.appendParagraph("Bendra dienos(pietūs-pavakariai) maitinimo kaina..... 7,70");
    body.appendParagraph("Jei pastebėjote klaidą mokėjimo pranešime, informuokite.");

    // Save and close the invoice document
    doc.saveAndClose();

    // Export the document as a Word file (docx)
    var docFile = DriveApp.getFileById(doc.getId());

    // Set the desired file name for the Word document
    var wordFileName = "Invoice for " + name + " - " + month + ".docx"; // Add ".docx" extension

    // Move the Word document to the desired folder
    folder.createFile(docFile.getBlob().setName(wordFileName));
  }
}

function onOpen() {
  var ui = SpreadsheetApp.getUi();
  ui.createMenu('Custom Menu')
    .addItem('Generate Invoices', 'generateInvoices')
    .addToUi();
}
Editor is loading...