Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

213
Views
La URL de búsqueda y reemplazo de Google Apps Script trunca 1 carácter en el enlace representado

Estoy usando el siguiente código para buscar direcciones URL en un documento de Google y convertirlas en enlaces activos. Funciona muy bien, excepto que el enlace corta el último carácter de la URL (por ejemplo, "https://link.com/123" se vincula como "https://link.com/12").

 function updateLinks() { var linkRegex = "https?:\/\/[^\\s]*"; //Open active doc var body = DocumentApp.getActiveDocument().getBody(); //Find URLs var link = body.findText(linkRegex); //Loop through the body finding texts matching the search pattern while (link != null) { // Get the link as an object var linkElement = link.getElement().asText(); // Get the positions of start and end var start = link.getStartOffset(); var end = link.getEndOffsetInclusive(); //slice only the link out of it var correctLink = linkElement.getText().slice(start, end); // Format link linkElement.setLinkUrl(start, end, correctLink); // Find next link = body.findText(linkRegex, link); } }

Estoy seguro de que esto es simple, pero soy un completo novato y agradecería cualquier consejo. ¡Gracias por adelantado!

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

tu codigo me esta funcionando

¿Cómo son tus archivos originales?

 function updateLinks() { var linkRegex = "https?:\/\/[^\\s]*"; var body = DocumentApp.getActiveDocument().getBody(); var link = body.findText(linkRegex); while (link != null) { var linkElement = link.getElement().asText(); var start = link.getStartOffset(); var end = link.getEndOffsetInclusive(); var correctLink = linkElement.getText().slice(start, end); linkElement.setLinkUrl(start, end, correctLink); link = body.findText(linkRegex, link); } }
about 4 years ago · Juan Pablo Isaza Report

0

Pensé que el motivo de su problema se debe a slice(start, end) de linkElement.getText().slice(start, end) . En este caso, se requiere que sea slice(start, end + 1) . Por lo tanto, modifíquelo de la siguiente manera y pruébelo nuevamente.

De:

 var correctLink = linkElement.getText().slice(start, end);

A:

 var correctLink = linkElement.getText().slice(start, end + 1);

O, cuando quiera usar linkRegex , ¿qué tal la siguiente modificación?

 var correctLink = linkElement.getText().match(new RegExp(linkRegex))[0];

Nota:

  • Como información adicional, cuando se incluyen varias URL en un párrafo, creo que la siguiente modificación podría ser útil. Por supuesto, en esta modificación, incluso cuando se incluye una sola URL en un párrafo, el script funciona.

    • De

       var start = link.getStartOffset(); var end = link.getEndOffsetInclusive(); //slice only the link out of it var correctLink = linkElement.getText().slice(start, end); // Format link linkElement.setLinkUrl(start, end, correctLink);
    • A

       var text = linkElement.getText(); var correctLinks = [...text.matchAll(new RegExp(linkRegex, "g"))]; correctLinks.forEach(obj => linkElement.setLinkUrl(obj.index, obj.index + obj[0].length - 1, obj[0]));

Referencia:

  • rodaja()
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!