QNA > H > How To Exceed The 50 Importxml Limit In Google Sheets

How to exceed the 50 importxml limit in Google Sheets

I created a custom import function that overcomes all limits of IMPORTXML I have a sheet using this in about 800 cells and it works great.

It makes use of Google Sheet’s custom scripts (Tools > Script editor…) and searches through content using regex instead of xpath.

  1. function importRegex(url, regexInput) { 
  2. var output = ''; 
  3. var fetchedUrl = UrlFetchApp.fetch(url, {muteHttpExceptions: true}); 
  4. if (fetchedUrl) { 
  5. var html = fetchedUrl.getContentText(); 
  6. if (html.length && regexInput.length) { 
  7. output = html.match(new RegExp(regexInput, 'i'))[1]; 
  8. // Grace period to avoid call limit 
  9. Utilities.sleep(1000); 
  10. return unescapeHTML(output); 

You can then use this function like any function.

=importRegex("https://example.com", "

(.*)<\/title>")

Of course, you can also reference cells.

=importRegex(A2, "

(.*)<\/title>")

If you don’t want to see HTML entities in the output, you can use this function.

  1. var htmlEntities = { 
  2. nbsp: ' ', 
  3. cent: '¢', 
  4. pound: '£', 
  5. yen: '¥', 
  6. euro: '€', 
  7. copy: '©', 
  8. reg: '®', 
  9. lt: '<', 
  10. gt: '>', 
  11. mdash: '–', 
  12. ndash: '-', 
  13. quot: '"', 
  14. amp: '&', 
  15. apos: '\'' 
  16. }; 
  17.  
  18. function unescapeHTML(str) { 
  19. return str.replace(/\&([^;]+);/g, function (entity, entityCode) { 
  20. var match; 
  21.  
  22. if (entityCode in htmlEntities) { 
  23. return htmlEntities[entityCode]; 
  24. } else if (match = entityCode.match(/^#x([\da-fA-F]+)$/)) { 
  25. return String.fromCharCode(parseInt(match[1], 16)); 
  26. } else if (match = entityCode.match(/^#(\d+)$/)) { 
  27. return String.fromCharCode(~~match[1]); 
  28. } else { 
  29. return entity; 
  30. }); 
  31. }; 

All together…

  1. function importRegex(url, regexInput) { 
  2. var output = ''; 
  3. var fetchedUrl = UrlFetchApp.fetch(url, {muteHttpExceptions: true}); 
  4. if (fetchedUrl) { 
  5. var html = fetchedUrl.getContentText(); 
  6. if (html.length && regexInput.length) { 
  7. output = html.match(new RegExp(regexInput, 'i'))[1]; 
  8. // Grace period to avoid call limit 
  9. Utilities.sleep(1000); 
  10. return unescapeHTML(output); 
  11.  
  12. var htmlEntities = { 
  13. nbsp: ' ', 
  14. cent: '¢', 
  15. pound: '£', 
  16. yen: '¥', 
  17. euro: '€', 
  18. copy: '©', 
  19. reg: '®', 
  20. lt: '<', 
  21. gt: '>', 
  22. mdash: '–', 
  23. ndash: '-', 
  24. quot: '"', 
  25. amp: '&', 
  26. apos: '\'' 
  27. }; 
  28.  
  29. function unescapeHTML(str) { 
  30. return str.replace(/\&([^;]+);/g, function (entity, entityCode) { 
  31. var match; 
  32.  
  33. if (entityCode in htmlEntities) { 
  34. return htmlEntities[entityCode]; 
  35. } else if (match = entityCode.match(/^#x([\da-fA-F]+)$/)) { 
  36. return String.fromCharCode(parseInt(match[1], 16)); 
  37. } else if (match = entityCode.match(/^#(\d+)$/)) { 
  38. return String.fromCharCode(~~match[1]); 
  39. } else { 
  40. return entity; 
  41. }); 
  42. }; 

Di Curry Saniger

È meglio Vainglory o Mobile Legends? :: Come ottenere che la ricerca di Google mi mostri 100 risultati per pagina
Link utili