Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.23 KB | None | 0 0
  1. function test() {
  2. var html = UrlFetchApp.fetch('https://docs.google.com/forms/d/1awKpg_diniayS6360kNXrcgihk36azQ3DJEaZqXDY7A/viewform?embedded=true').getContentText();
  3. var form = html.match(/<form(.*?)</form>/g);
  4. Logger.log(form);
  5. }
  6.  
  7. function doGet() {
  8. // En vez de obtener el html con UrlFetchApp.fetch(), se usa esto a modo de ejemplo
  9. var html = '<html>'
  10. + ' <body>'
  11. + ' <p>Texto a borrar</p>'
  12. + ' <form>'
  13. + ' <input type="radio" name="sexo" value="masculino" checked="1"> Masculino<br>'
  14. + ' <input type="radio" name="sexo" value="femenino"> Femenino'
  15. + ' </form>'
  16. + ' <p>Esto no debe aparecer</p>'
  17. + ' <form>'
  18. + ' Segundo form <input type="button" value="Funciona">'
  19. + ' </form>'
  20. + ' </body>'
  21. + '</html>';
  22.  
  23. // Se crea el documento
  24. var doc = Xml.parse(html, true); //Xml.parse está obsoleto pero sigue funcionando y mejor que XmlService
  25. var body = doc.html.body.toXmlString(); //truco para que funcione XmlService (sino no acepta HTML que no cumple como XML)
  26. var atom = XmlService.getNoNamespace();
  27. doc = XmlService.parse(body);
  28. var root = doc.getRootElement();
  29. var i, resultado = '';
  30.  
  31. // Se obtienen todos los forms
  32. var forms = getElementsByTagName(root, 'form');
  33.  
  34. // Se unen en un string
  35. for(i in forms) resultado += XmlService.getRawFormat().format(forms[i]);
  36.  
  37. // Envíar resultado como salida del script
  38. return HtmlService.createHtmlOutput(resultado);
  39. }
  40.  
  41. function getElementsByTagName(element, tagName) {
  42. // Fuente: https://sites.google.com/site/scriptsexamples/learn-by-example/parsing-html
  43. var data = [];
  44. var descendants = element.getDescendants();
  45. for(i in descendants) {
  46. var elt = descendants[i].asElement();
  47. if( elt !=null && elt.getName()== tagName) data.push(elt);
  48. }
  49. return data;
  50. }
  51.  
  52. $('form').each(function(){
  53. var html = $( this ).html();
  54. })
  55.  
  56. function doGet() {
  57. var html = UrlFetchApp.fetch('https://docs.google.com/forms/d/1awKpg_diniayS6360kNXrcgihk36azQ3DJEaZqXDY7A/viewform?embedded=true').getContentText();
  58. var output = html.match(/<form[sS]*form>/g);
  59. return HtmlService.createHtmlOutput(output);
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement