Bots de conversa de Telegram amb Google Apps Script

Bots de conversa Exemples Dades pràctiques   Recursos CITCEA
Google Apps Script Projectes Interacció   Inici

Rutes

Donades una sèrie de localitzacions, el bot de conversa prova totes les permutacions possibles d'ordre d'aquests punts i escull la combinació que dona com a resultat la ruta més ràpida. D'aquesta manera, el bot de conversa retorna un enllaç a Google Maps amb la ruta més òptima així com el seu temps d'itinerari i els quilòmetres a recórrer en diversos mitjans de transport com ara a peu, en bicicleta o en vehicle de motor (cotxe, autobús, etc.). A més, en el cas de l'ús d'un vehicle de motor, el bot de conversa mostra la quantitat de benzina o gasoil consumit així com la contaminació emesa durant el trajecte.

El programa de l'script és el següent:

var token = "^^68339729:AAENLsuUVRDcbnzyU9cUXuvcxQxtJUxkSdw"; // API Token de Telegram 
var telegramUrl = "https://api.telegram.org/bot" + token;  // Url que comunica el nostre bot amb Telegram 
// Adreça de l'script (ens la donen en el moment de publicar-lo)
var webAppUrl = "https://script.google.com/macros/s/^^fycby_AnDU732CH1dM1_Jt5740SsqGV7QrDrRMfamXmcbQ9Im96_47_u2PlI4sCJPhUr1o/exec";
var IdFull = "^^BkuHJxS8E3jKuASUjO4br0cYLNyIKPi8kEMVgEUPZE";
function setWebhook() {
  var url = telegramUrl + "/setWebhook?url=" + webAppUrl;
  var response = UrlFetchApp.fetch(url);
}
function sendText(chatId,text_env){  
  var cont = {
    method: "post",
    payload: {
      method: "sendMessage",
      chat_id: String(chatId),
      text: text_env,
      parse_mode: "HTML"
    }
  }
  UrlFetchApp.fetch(telegramUrl + '/', cont);
}
// Funció que s'executa quan l'script rep una ordre post
function doPost(e){ // Funció que rep les dades enviades, mitjançant post, per Telegram
  var fet = 0;
  var resposta = '';
  var dades = JSON.parse(e.postData.contents);  // Tracta les dades rebudes com a JSON i les guarda a dades
  var text = dades.message.text;  // Recupera el text del missatge
  var id = dades.message.chat.id;  // Recupera l'identificador de la finestra des de la que s'ha enviat
  var comanda = text.split("@");  // Separa per les @
  var cmd = comanda[0];  // Comanda
  var par1 = comanda[1];  // Primer paràmetre
  var id_usuari = dades.message.from.id; // Recupera l'identificador de l'usuari que ha escrit el missatge
  var id_missatge = dades.message.message_id; // Recupera l'identificador del missatge
  var lang = dades.message.from.language_code ;  // Llengua que l'usuari té configurada a Telegram 
  var nom_usuari = dades.message.from.first_name ;  // Recupera el nom de l'usuari que ha enviat el missatge
  var posicio = dades.message.location;  // Recupera la localizació de l'usuari, si és possible
  var sh = SpreadsheetApp.openById(IdFull);
  var sheet = sh.getSheets();
  var sheet_direcciones = sh.getSheetByName('Direcciones');
  var sheet_ruta = sh.getSheetByName('RutaP');
  var sheet_rutam = sh.getSheetByName('RutaTP');
  var sheet_rutac = sh.getSheetByName('RutaC');
  if (cmd == '/start'){
    resposta = "Es vol fer la ruta a peu, en vehicle privat de motor o en bicicleta?\n";
    resposta = resposta +"- Utilitzar comanda calculapeu si es vol fer a peu\n";
    resposta = resposta +"- Utilitzar comanda calculamotor si es vol fer en vehicle privat de motor\n";
    resposta = resposta +"- Utilitzar comanda calculabici si es vol fer en bicicleta\n";
    resposta = resposta +"- Utilitzar comanda calculaopcions si es vol comprovar els resultats de totes les opcions possibles";
    sendText(id,resposta)
    fet = 1;
  }
  if (cmd == '/llistat'){
    var llista = "/llistat - Llista de les comandes disponibles\n";  // Text que es retornarà a Telegram
    llista = llista + "/guardar - Guarda un valor al full de càlcul\n";
    llista = llista + "/llegir - Llegeix el darrer valor guardat\n";
    llista = llista + "/neteja - Esborra la ruta previa\n";
    llista = llista + "/calculapeu - Calcula el temps i distància de la ruta feta a peu més ràpida possible\n";
    llista = llista + "/calculamotor - Calcula el temps i distància de la ruta feta en vehicle privat de motor més ràpida possible\n";
    llista = llista + "/calculabici - Calcula el temps i distància de la ruta feta en bicicleta més ràpida possible\n";
    llista = llista + "/calculaopcions - Calcula totes les opcions possibles per fer el trajecte";
    sendText(id,llista);
    fet = 1;
  }
  if (cmd == '/guardar'){
    if (par1 == undefined){  // Si no hi ha el paràmetre
      resposta = "No s'ha rebut cap dada per guardar\n";
      resposta = resposta + "La sintaxi és /guardar@dada\n";
      resposta = resposta + "Per exemple: /guardar@22,4";
    } else {
      var camps = new Array(1);  // Valors per guardar a la taula
      camps[0] = par1;  // Valor que cal guardar
      sheet[0].appendRow(camps);   // Afegeix una fila amb la llista de dades en format matriu al primer full
      resposta = 'Valor guardat';
    }
    sendText(id,resposta);
    fet = 1;
  }
  if (cmd == '/llegir'){
    var full = sheet_direcciones.getDataRange().getValues();  // Agafem els valors
    var rang = sheet_direcciones.getLastRow();  // Índex de la darrera filera
    if (rang > 1){    // Si és 1 només hi ha els títols
      var filera = full[rang -1];    // Agafem la darrera filera
      resposta = filera[0];  // Dada que hi ha a la filera
    } else {
      resposta = "No hi ha cap dada guardada";
    }
    sendText(id,resposta);
    fet = 1;
  }
  if (cmd == '/calculaopcions'){
    var filaInicial = 2;
    var ultimaFila = sheet_direcciones.getLastRow();
    var direccionesDataRange = sheet_direcciones.getRange(filaInicial, 1,ultimaFila, 1);
    var direccionesData = direccionesDataRange.getValues();
    var instrucciones = Maps.newDirectionFinder();
    if (direccionesData.length > 2) {
      for (var i = 0; i < (direccionesData.length - 1); i++) {
        var direccion = direccionesData[i][0];
        // Solamente haremos algo si no están vacías las coordenadas, 
        // para no generar errores inesperados
        if (direccion != "") {
          // Si es el primer punto, ponemos el origen
          if (i == 0) {
            instrucciones.setOrigin(direccion);
            // Si es el último punto, ponemos el destino
          } else if (i == (direccionesData.length - 2)) {
            instrucciones.setDestination(direccion);
            // Si es un punto intermedio, lo agregamos como waypoint 
          } else {
            instrucciones.addWaypoint(direccion).setOptimizeWaypoints(true);
          }
        }
      }
    } else {
      SpreadsheetApp.getActive().toast('Se necesitan al menos dos direcciones o coordenadas.', '?? Error');
      return;
    }
    instrucciones.setMode(Maps.DirectionFinder.Mode.WALKING);
    instrucciones = instrucciones.getDirections();
    var ruta = instrucciones.routes[0];
    var filaInicial = 2;
    for(var i = 0; i < ruta.legs.length; i++) {
      var leg = ruta.legs[i];
      var distancia = (leg.distance.value) / 1000; // m a km
      var tiempo = (leg.duration.value) / 60; // segundos a min
      var de = leg.start_address;
      var a = leg.end_address;
      sheet_ruta.getRange(filaInicial + i, 1).setValue(de); // columna De
      sheet_ruta.getRange(filaInicial + i, 2).setValue(a); // columna A
      sheet_ruta.getRange(filaInicial + i, 3).setValue(distancia); // columna Distancia (km)
      sheet_ruta.getRange(filaInicial + i, 4).setValue(tiempo); // columna Tiempo (min)
      sheet_ruta.getRange(filaInicial + i+1, 1).setValue(a);
    }
    var celdaminp = sheet_ruta.getRange(3, 9);
    var celdahp = sheet_ruta.getRange(4, 8);
    var celdakmp = sheet_ruta.getRange(7, 7);
    var filaInicial = 2;
    var ultimaFila = sheet_direcciones.getLastRow();
    var direccionesDataRange = sheet_direcciones.getRange(filaInicial, 1,ultimaFila, 1);
    var direccionesData = direccionesDataRange.getValues();
    var instrucciones = Maps.newDirectionFinder();
    if (direccionesData.length > 2) {
      for (var i = 0; i < (direccionesData.length - 1); i++) {
        var direccion = direccionesData[i][0];
        // Solamente haremos algo si no están vacías las coordenadas, 
        // para no generar errores inesperados
        if (direccion != "") {
          // Si es el primer punto, ponemos el origen
          if (i == 0) {
            instrucciones.setOrigin(direccion);
            // Si es el último punto, ponemos el destino
          } else if (i == (direccionesData.length - 2)) {
            instrucciones.setDestination(direccion);
            // Si es un punto intermedio, lo agregamos como waypoint 
          } else {
            instrucciones.addWaypoint(direccion).setOptimizeWaypoints(true);
          }
        }
      }
    } else {
      SpreadsheetApp.getActive().toast('Se necesitan al menos dos direcciones o coordenadas.', '?? Error');
      return;
    }
    instrucciones.setMode(Maps.DirectionFinder.Mode.DRIVING);
    instrucciones = instrucciones.getDirections();
    var ruta = instrucciones.routes[0];
    var filaInicial = 2;
    for(var i = 0; i < ruta.legs.length; i++) {
      var leg = ruta.legs[i];
      var distancia = (leg.distance.value) / 1000; // m a km
      var tiempo = (leg.duration.value) / 60; // segundos a min
      var de = leg.start_address;
      var a = leg.end_address;
      sheet_rutam.getRange(filaInicial + i, 1).setValue(de); // columna De
      sheet_rutam.getRange(filaInicial + i, 2).setValue(a); // columna A
      sheet_rutam.getRange(filaInicial + i, 3).setValue(distancia); // columna Distancia (km)
      sheet_rutam.getRange(filaInicial + i, 4).setValue(tiempo); // columna Tiempo (min)
      sheet_rutam.getRange(filaInicial + i+1, 1).setValue(a);
    }
    var filaInicial = 2;
    var ultimaFila = sheet_direcciones.getLastRow();
    var direccionesDataRange = sheet_direcciones.getRange(filaInicial, 1,ultimaFila, 1);
    var direccionesData = direccionesDataRange.getValues();
    var instrucciones = Maps.newDirectionFinder();
    var celdaminc = sheet_rutam.getRange(3, 9);
    var celdahc = sheet_rutam.getRange(4, 8);
    var celdakmc = sheet_rutam.getRange(7, 7);
    var gastgas = sheet_rutam.getRange(7, 10);
    var gastdies = sheet_rutam.getRange(10, 10);
    var contgas = sheet_rutam.getRange(14, 10);
    var contdies = sheet_rutam.getRange(15, 10);
    var contelec = sheet_rutam.getRange(16, 10);
    var filaInicial = 2;
    var ultimaFila = sheet_direcciones.getLastRow();
    var direccionesDataRange = sheet_direcciones.getRange(filaInicial, 1,ultimaFila, 1);
    var direccionesData = direccionesDataRange.getValues();
    var instrucciones = Maps.newDirectionFinder();
    if (direccionesData.length > 2) {
      for (var i = 0; i < (direccionesData.length - 1); i++) {
        var direccion = direccionesData[i][0];
        // Solamente haremos algo si no están vacías las coordenadas, 
        // para no generar errores inesperados
        if (direccion != "") {
          // Si es el primer punto, ponemos el origen
          if (i == 0) {
            instrucciones.setOrigin(direccion);
            // Si es el último punto, ponemos el destino
          } else if (i == (direccionesData.length - 2)) {
            instrucciones.setDestination(direccion);
            // Si es un punto intermedio, lo agregamos como waypoint 
          } else {
            instrucciones.addWaypoint(direccion).setOptimizeWaypoints(true);
          }
        }
      }
    } else {
      SpreadsheetApp.getActive().toast('Se necesitan al menos dos direcciones o coordenadas.', '?? Error');
      return;
    }
    instrucciones.setMode(Maps.DirectionFinder.Mode.BICYCLING);
    instrucciones = instrucciones.getDirections();
    var ruta = instrucciones.routes[0];
    var filaInicial = 2;
    for(var i = 0; i < ruta.legs.length; i++) {
      var leg = ruta.legs[i];
      var distancia = (leg.distance.value) / 1000; // m a km
      var tiempo = (leg.duration.value) / 60; // segundos a min
      var de = leg.start_address;
      var a = leg.end_address;
      sheet_rutac.getRange(filaInicial + i, 1).setValue(de); // columna De
      sheet_rutac.getRange(filaInicial + i, 2).setValue(a); // columna A
      sheet_rutac.getRange(filaInicial + i, 3).setValue(distancia); // columna Distancia (km)
      sheet_rutac.getRange(filaInicial + i, 4).setValue(tiempo); // columna Tiempo (min)
      sheet_rutac.getRange(filaInicial + i+1, 1).setValue(a);
    }
    var filaInicial = 2;
    var ultimaFila = sheet_direcciones.getLastRow();
    var direccionesDataRange = sheet_direcciones.getRange(filaInicial, 1,ultimaFila, 1);
    var direccionesData = direccionesDataRange.getValues();
    var instrucciones = Maps.newDirectionFinder();
    var celdaminb = sheet_rutac.getRange(3, 9);
    var celdahb = sheet_rutac.getRange(4, 8);
    var celdakmb = sheet_rutac.getRange(7, 7);
    var celdamapa = sheet_ruta.getRange(2,5);
    total = "Ruta a peu:\n" + celdahp.getValue() + " hores i " + celdaminp.getValue() + " minuts\n";
    total = total + celdakmp.getValue() + " km\n" + "\n" + "\n" + "Ruta en bici:\n";
    total = total + celdahb.getValue() + " hores i " + celdaminb.getValue() + " minuts\n";
    total = total + celdakmb.getValue() + " km\n" + "\n" + "\n" + "Ruta a motor:\n";
    total = total + celdahc.getValue() + " hores i " + celdaminc.getValue() + " minuts\n";
    total = total + celdakmc.getValue() + " km\n" + "Benzina utilitzada: " + gastgas.getValue();
    total = total + "litres\n" + "Diesel utilitzat: " + gastdies.getValue() + "litres\n";
    total = total + "Contaminació benzina:\n" + contgas.getValue() + "g de CO2\n";
    total = total + "Contaminació diesel:\n" + contdies.getValue() + " g de CO2\n"
    total = total + "Contaminació electric:\n" + contelec.getValue() + " g de CO2\n";
    total = total + "\n" + "* (Dades de utilització i contaminació aproximades)";
    total = total + "\n" + "\n" + "Mapa ruta:\n" + celdamapa.getValue();
    sendText(id,total);
    fet = 1;
  }
  if (cmd == '/neteja'){
    var filaInicial = 2;
    var ultimaFila = sheet_ruta.getLastRow();
    var rutaDataRange = sheet_ruta.getRange(filaInicial, 1,ultimaFila, 1);
    var rutaSheetData = rutaDataRange.getValues();
    for(var i = 0; i < rutaSheetData.length; i++) {
      sheet_ruta.getRange(filaInicial + i, 1).setValue('');
      sheet_ruta.getRange(filaInicial + i, 2).setValue('');
      sheet_ruta.getRange(filaInicial + i, 3).setValue('');
      sheet_ruta.getRange(filaInicial + i, 4).setValue('');
    }
    var filaInicial = 2;
    var ultimaFila = sheet_direcciones.getLastRow();
    var rutaDataRange = sheet_direcciones.getRange(filaInicial, 1,ultimaFila, 1);
    var rutaSheetData = rutaDataRange.getValues();
    for(var i = 0; i < rutaSheetData.length; i++) {
      sheet_direcciones.getRange(filaInicial + i, 1).setValue('');
      sheet_direcciones.getRange(filaInicial + i, 2).setValue('');
      sheet_direcciones.getRange(filaInicial + i, 3).setValue('');
      sheet_direcciones.getRange(filaInicial + i, 4).setValue('');
    }
    var filaInicial = 2;
    var ultimaFila = sheet_rutam.getLastRow();
    var rutaDataRange = sheet_rutam.getRange(filaInicial, 1,ultimaFila, 1);
    var rutaSheetData = rutaDataRange.getValues();
    for(var i = 0; i < rutaSheetData.length; i++) {
      sheet_rutam.getRange(filaInicial + i, 1).setValue('');
      sheet_rutam.getRange(filaInicial + i, 2).setValue('');
      sheet_rutam.getRange(filaInicial + i, 3).setValue('');
      sheet_rutam.getRange(filaInicial + i, 4).setValue('');
    }
    var filaInicial = 2;
    var ultimaFila = sheet_rutac.getLastRow();
    var rutaDataRange = sheet_rutac.getRange(filaInicial, 1,ultimaFila, 1);
    var rutaSheetData = rutaDataRange.getValues();
    for(var i = 0; i < rutaSheetData.length; i++) {
      sheet_rutac.getRange(filaInicial + i, 1).setValue('');
      sheet_rutac.getRange(filaInicial + i, 2).setValue('');
      sheet_rutac.getRange(filaInicial + i, 3).setValue('');
      sheet_rutac.getRange(filaInicial + i, 4).setValue('');
    }
    sendText(id,'Neteja completada')
    fet = 1;   
  }
  if (cmd == '/calculapeu'){
    var filaInicial = 2;
    var ultimaFila = sheet_direcciones.getLastRow();
    var direccionesDataRange = sheet_direcciones.getRange(filaInicial, 1,ultimaFila, 1);
    var direccionesData = direccionesDataRange.getValues();
    var instrucciones = Maps.newDirectionFinder();
    if (direccionesData.length > 2) {
      for (var i = 0; i < (direccionesData.length - 1); i++) {
        var direccion = direccionesData[i][0];
        // Solamente haremos algo si no están vacías las coordenadas, 
        // para no generar errores inesperados
        if (direccion != "") {
          // Si es el primer punto, ponemos el origen
          if (i == 0) {
            instrucciones.setOrigin(direccion);
            // Si es el último punto, ponemos el destino
          } else if (i == (direccionesData.length - 2)) {
            instrucciones.setDestination(direccion);
            // Si es un punto intermedio, lo agregamos como waypoint 
          } else {
            instrucciones.addWaypoint(direccion).setOptimizeWaypoints(true);
          }
        }
      }
    } else {
      SpreadsheetApp.getActive().toast('Se necesitan al menos dos direcciones o coordenadas.', '?? Error');
      return;
    }
    instrucciones.setMode(Maps.DirectionFinder.Mode.WALKING);
    instrucciones = instrucciones.getDirections();
    var ruta = instrucciones.routes[0];
    var filaInicial = 2;
    for(var i = 0; i < ruta.legs.length; i++) {
      var leg = ruta.legs[i];
      var distancia = (leg.distance.value) / 1000; // m a km
      var tiempo = (leg.duration.value) / 60; // segundos a min
      var de = leg.start_address;
      var a = leg.end_address;
      sheet_ruta.getRange(filaInicial + i, 1).setValue(de); // columna De
      sheet_ruta.getRange(filaInicial + i, 2).setValue(a); // columna A
      sheet_ruta.getRange(filaInicial + i, 3).setValue(distancia); // columna Distancia (km)
      sheet_ruta.getRange(filaInicial + i, 4).setValue(tiempo); // columna Tiempo (min)
      sheet_ruta.getRange(filaInicial + i+1, 1).setValue(a);
    }
    var celdamin = sheet_ruta.getRange(3, 9);
    var celdah = sheet_ruta.getRange(4, 8);
    var celdakm = sheet_ruta.getRange(7, 7);
    var celdamapa = sheet_ruta.getRange(2,5);
    total = celdah.getValue() + " hores i " + celdamin.getValue() + " minuts\n" + celdakm.getValue();
    total = total + " km\n" + "\n" + "\n" + "Mapa ruta:\n" + celdamapa.getValue();
    sendText(id,total);
    fet = 1;
  }
  if (cmd == '/calculamotor'){
    var filaInicial = 2;
    var ultimaFila = sheet_direcciones.getLastRow();
    var direccionesDataRange = sheet_direcciones.getRange(filaInicial, 1,ultimaFila, 1);
    var direccionesData = direccionesDataRange.getValues();
    var instrucciones = Maps.newDirectionFinder();
    if (direccionesData.length > 2) {
      for (var i = 0; i < (direccionesData.length - 1); i++) {
        var direccion = direccionesData[i][0];
        // Solamente haremos algo si no están vacías las coordenadas, 
        // para no generar errores inesperados
        if (direccion != "") {
          // Si es el primer punto, ponemos el origen
          if (i == 0) {
            instrucciones.setOrigin(direccion);
            // Si es el último punto, ponemos el destino
          } else if (i == (direccionesData.length - 2)) {
            instrucciones.setDestination(direccion);
            // Si es un punto intermedio, lo agregamos como waypoint 
          } else {
            instrucciones.addWaypoint(direccion).setOptimizeWaypoints(true);
          }
        }
      }
    } else {
      SpreadsheetApp.getActive().toast('Se necesitan al menos dos direcciones o coordenadas.', '?? Error');
      return;
    }
    instrucciones.setMode(Maps.DirectionFinder.Mode.DRIVING);
    instrucciones = instrucciones.getDirections();
    var ruta = instrucciones.routes[0];
    var filaInicial = 2;
    for(var i = 0; i < ruta.legs.length; i++) {
      var leg = ruta.legs[i];
      var distancia = (leg.distance.value) / 1000; // m a km
      var tiempo = (leg.duration.value) / 60; // segundos a min
      var de = leg.start_address;
      var a = leg.end_address;
      sheet_rutam.getRange(filaInicial + i, 1).setValue(de); // columna De
      sheet_rutam.getRange(filaInicial + i, 2).setValue(a); // columna A
      sheet_rutam.getRange(filaInicial + i, 3).setValue(distancia); // columna Distancia (km)
      sheet_rutam.getRange(filaInicial + i, 4).setValue(tiempo); // columna Tiempo (min)
      sheet_rutam.getRange(filaInicial + i+1, 1).setValue(a);
    }
    var filaInicial = 2;
    var ultimaFila = sheet_direcciones.getLastRow();
    var direccionesDataRange = sheet_direcciones.getRange(filaInicial, 1,ultimaFila, 1);
    var direccionesData = direccionesDataRange.getValues();
    var instrucciones = Maps.newDirectionFinder();
    var celdamin = sheet_rutam.getRange(3, 9)
    var celdah = sheet_rutam.getRange(4, 8)
    var celdakm = sheet_rutam.getRange(7, 7)
    var celdamapa = sheet_rutam.getRange(2,5)
    total = celdah.getValue() + " hores i " + celdamin.getValue() + " minuts\n" + celdakm.getValue();
    total = total + " km\n" + "\n" + "\n" + "Mapa ruta:\n" + celdamapa.getValue();
    sendText(id,total);
    fet = 1;
  }
  if (cmd == '/calculabici'){
    var filaInicial = 2;
    var ultimaFila = sheet_direcciones.getLastRow();
    var direccionesDataRange = sheet_direcciones.getRange(filaInicial, 1,ultimaFila, 1);
    var direccionesData = direccionesDataRange.getValues();
    var instrucciones = Maps.newDirectionFinder();
    if (direccionesData.length > 2) {
      for (var i = 0; i < (direccionesData.length - 1); i++) {
        var direccion = direccionesData[i][0];
        // Solamente haremos algo si no están vacías las coordenadas, 
        // para no generar errores inesperados
        if (direccion != "") {
          // Si es el primer punto, ponemos el origen
          if (i == 0) {
            instrucciones.setOrigin(direccion);
            // Si es el último punto, ponemos el destino
          } else if (i == (direccionesData.length - 2)) {
            instrucciones.setDestination(direccion);
            // Si es un punto intermedio, lo agregamos como waypoint 
          } else {
            instrucciones.addWaypoint(direccion).setOptimizeWaypoints(true);
          }
        }
      }
    } else {
      SpreadsheetApp.getActive().toast('Se necesitan al menos dos direcciones o coordenadas.', '?? Error');
      return;
    }
    instrucciones.setMode(Maps.DirectionFinder.Mode.BICYCLING);
    instrucciones = instrucciones.getDirections();
    var ruta = instrucciones.routes[0];
    var filaInicial = 2;
    for(var i = 0; i < ruta.legs.length; i++) {
      var leg = ruta.legs[i];
      var distancia = (leg.distance.value) / 1000; // m a km
      var tiempo = (leg.duration.value) / 60; // segundos a min
      var de = leg.start_address;
      var a = leg.end_address;
      sheet_rutac.getRange(filaInicial + i, 1).setValue(de); // columna De
      sheet_rutac.getRange(filaInicial + i, 2).setValue(a); // columna A
      sheet_rutac.getRange(filaInicial + i, 3).setValue(distancia); // columna Distancia (km)
      sheet_rutac.getRange(filaInicial + i, 4).setValue(tiempo); // columna Tiempo (min)
      sheet_rutac.getRange(filaInicial + i+1, 1).setValue(a);
    }
    var filaInicial = 2;
    var ultimaFila = sheet_direcciones.getLastRow();
    var direccionesDataRange = sheet_direcciones.getRange(filaInicial, 1,ultimaFila, 1);
    var direccionesData = direccionesDataRange.getValues();
    var instrucciones = Maps.newDirectionFinder();
    var celdamin = sheet_rutac.getRange(3, 9);
    var celdah = sheet_rutac.getRange(4, 8);
    var celdakm = sheet_rutac.getRange(7, 7);
    var celdamapa = sheet_rutac.getRange(2,5);
    total = celdah.getValue() + " hores i " + celdamin.getValue() + " minuts\n" + celdakm.getValue();
    total = total + " km\n" + "\n" + "\n" + "Mapa ruta:\n" + celdamapa.getValue();
    sendText(id,total);
    fet = 1;
  }
  if (fet == 0){
    resposta = "Comanda incorrecta";
    sendText(id,resposta);
  }
}

 

 

 

 

 

 

 

 

 

 

Llicència de Creative Commons
Aquesta obra d'Oriol Boix està llicenciada sota una llicència no importada Reconeixement-NoComercial-SenseObraDerivada 3.0.