Advertisement
mrnavaone

5

May 22nd, 2020
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function doGet(e) {
  2.    Logger.log( Utilities.jsonStringify(e) );
  3.  
  4.   if (!e.parameter.page) {
  5.  
  6.     return HtmlService.createTemplateFromFile('index2').evaluate();
  7.   }
  8.  
  9.   var template = HtmlService.createTemplateFromFile(e.parameter.page)
  10.   template.action = ScriptApp.getService().getUrl();
  11.   return template.evaluate();
  12. }
  13.  
  14. function addUser(id,name,nota1,nota2,nota3) {
  15.   var url = "https://docs.google.com/spreadsheets/d/1Yn-z7X-mb9TWaxjWWps79qb_v68h9KHFAWDMn7jLfU0/edit#gid=0";
  16.   var ss = SpreadsheetApp.openByUrl(url);
  17.   var ws = ss.getSheetByName("notas");
  18.  
  19.   ws.appendRow([id,name,nota1,nota2,nota3]);
  20.  
  21.  
  22. }
  23.  
  24.  
  25.  
  26. //Called from the client with form data, basic validation for blank values
  27. function formSubmit(formData){
  28.   for(var field in formData){
  29.     if(formData[field] == ''){
  30.       return {success: false, message: field + 'No se pueden enviar campos vacios'}
  31.     }
  32.   }
  33.   return {success: true, message: 'Mensaje enviado con exito'};
  34. }
  35.  
  36. function menu(){
  37.   var ui = SpreadsheetApp.getUi();
  38.   ui.createMenu("Procesos")
  39.     .addItem('Calcular Notas', 'CalculoNotas')
  40.     .addItem('Enviar resultados', 'spreadsheetToPdf')
  41.     .addToUi();
  42. }
  43.  
  44.  
  45. function CalculoNotas() {
  46.    
  47.  const sps = SpreadsheetApp.getActive();
  48.   var sheet =  sps.getSheets();
  49.   var data =  sheet[0].getDataRange().getValues();
  50. var contacts = sps.getSheetByName("percent");
  51. var numRows = contacts.getLastRow();
  52. const percent1 = contacts.getRange(1, 1, numRows, 1).getValues();  
  53. const percent2 = contacts.getRange(1, 2, numRows, 1).getValues();      
  54. const percent3 = contacts.getRange(1, 3, numRows, 1).getValues();
  55.  
  56.  
  57.  
  58.  
  59.     for (i in data)
  60.   {
  61.     var row = data[i];
  62.     var Nota1 = row[2];
  63.     var Nota2 = row[3];
  64.     var Nota3 = row[4];
  65.     var Apro =  row[8];
  66.     var Total = (Nota1 * percent1) + (Nota2 * percent2) + (Nota3 * percent3);
  67.  
  68.    
  69.    if (Total >= 6 )
  70.     {
  71.    
  72.      var StarRow = 1 + +i;
  73.      sheet[0].getRange(StarRow, 7).setValue("Aprobado");
  74.      }
  75.       else
  76.       {
  77.      
  78.      var StarRow = 1 + +i;
  79.      sheet[0].getRange(StarRow, 7).setValue("Reprobado");
  80.       }
  81.    
  82.     var StarRow = 1 + +i;
  83.      sheet[0].getRange(1, 6).setValue("Total"); //Agrega el  texto: Total en la columna 5
  84.      sheet[0].getRange(1, 7).setValue("Estado"); //Agrega el texto:Estado en la columna 6
  85.      sheet[0].getRange(1, 8).setValue("Aprobados").setBackground('Green'); //Agrega el texto:Estado en la columna 7
  86.      sheet[0].getRange(2, 8).setValue("Reprobados").setBackground('Red'); //Agrega el texto:Estado en la columna 8
  87.     sheet[0].getRange(StarRow, 6).setValue(Total); //6
  88.    
  89.     //conteo aprobados vrs reprobados
  90.   sheet[0].getRange("I1").setFormula('=COUNTIF(G:G;"Aprobado")');
  91.   sheet[0].getRange("I2").setFormula('=COUNTIF(G:G;"Reprobado")');
  92.    
  93.    
  94.   }
  95.  
  96. }
  97.  
  98.  
  99.  
  100. function spreadsheetToPdf() {
  101.  
  102.   const ss = SpreadsheetApp.getActiveSpreadsheet();
  103.   const subject = `PDF Generado por ${ss.getName()}`;
  104.   const body = "Enviado con [Email SpreadSheet] Reporte de Notas";
  105.   const url = 'https://docs.google.com/spreadsheets/d/SS_ID/export?'.replace('SS_ID', ss.getId());
  106.   var contacts = ss.getSheetByName("contacto");
  107.   var numRows = contacts.getLastRow();
  108.   const email = contacts.getRange(1, 1, numRows, 1).getValues();
  109.   const exportOptions =
  110.     'exportFormat=pdf&format=pdf' + // export as pdf / csv / xls / xlsx
  111.     '&size=letter' + // paper size legal / letter / A4
  112.     '&portrait=false' + // orientation, false for landscape
  113.     '&fitw=true&source=tuto' + // fit to page width, false for actual size
  114.     '&sheetnames=false&printtitle=false' + // hide optional headers and footers
  115.     '&pagenumbers=false&gridlines=false' + // hide page numbers and gridlines
  116.     '&fzr=false' + // do not repeat row headers (frozen rows) on each page
  117.     '&gid='; // the sheet's Id
  118.  
  119.   const token = ScriptApp.getOAuthToken();
  120.   const sheets = ss.getSheets();
  121.   const blobs = [];
  122.  
  123.   for (let i = 0; i < sheets.length; i += 1) {
  124.     const response = UrlFetchApp.fetch(url + exportOptions + sheets[i].getSheetId(), {
  125.       headers: {
  126.         Authorization: `Bearer ${token}`
  127.       }
  128.     });
  129.  
  130.  
  131.     blobs[i] = response.getBlob().setName(`${sheets[i].getName()}.pdf`);
  132.   }
  133.  
  134.  
  135.   const zipBlob = Utilities.zip(blobs).setName(`${ss.getName()}.zip`);
  136.  
  137.  
  138.   DriveApp.createFile(zipBlob);
  139.  
  140.   Logger.log(`Storage Space used: ${DriveApp.getStorageUsed()}`);
  141.  
  142.   if (MailApp.getRemainingDailyQuota() > 0)
  143.     GmailApp.sendEmail(email, subject, body, {
  144.       htmlBody: body,
  145.       attachments: [zipBlob]
  146.     });
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement