Advertisement
incomestreamsurfer

Internal link script

Jan 26th, 2023
38,488
14
Never
6
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.60 KB | None | 14 0
  1. const addLinks = (searchPhrase, hyperlink) => {
  2. const document = DocumentApp.getActiveDocument();
  3. const body = document.getBody();
  4. let search = null;
  5. let count = 0;
  6. while (count < 1 && (search = body.findText(searchPhrase, search))) {
  7. const searchElement = search.getElement();
  8. const startIndex = search.getStartOffset();
  9. const endIndex = search.getEndOffsetInclusive();
  10. searchElement.asText().setLinkUrl(startIndex, endIndex, hyperlink);
  11. count++;
  12. }
  13. document.saveAndClose();
  14. };
  15.  
  16. addLinks("Kiton", "https://2men.it/collections/kiton/");
Advertisement
Comments
  • Dazentaki
    2 years
    # text 0.77 KB | 4 0
    1. function addLinksFromSheet() {
    2. var sheet = SpreadsheetApp.openById("YOUR_SPREADSHEET_ID").getSheetByName("Sheet1");
    3. var data = sheet.getDataRange().getValues();
    4.  
    5. var document = DocumentApp.getActiveDocument();
    6. var body = document.getBody();
    7.  
    8. for (var i = 0; i < data.length; i++) {
    9. var searchPhrase = data[i][0];
    10. var hyperlink = data[i][1];
    11.  
    12. var search = null;
    13. var count = 0;
    14. while (count < 1 && (search = body.findText(searchPhrase, search))) {
    15. var searchElement = search.getElement();
    16. var startIndex = search.getStartOffset();
    17. var endIndex = search.getEndOffsetInclusive();
    18. searchElement.asText().setLinkUrl(startIndex, endIndex, hyperlink);
    19. count++;
    20. }
    21. }
    22.  
    23. document.saveAndClose();
    24. }
    25.  
  • Dazentaki
    2 years
    # text 0.17 KB | 3 0
    1. Code on top is an upgrade in case one would like to create a database in google sheet of the searchPhrase/Hyperlink and use them automatically in multiple blog articles.
  • agimenez
    2 years
    # text 0.20 KB | 0 0
    1. The code is not working, I don't know what I am missing, is giving me the error:
    2. Error
    3. Exception: Invalid argument: searchPattern
    4. addLinks @ Code.gs:6
    5.  
    6. Any idea of what I can do to fix it?
    7.  
    8. Thank you!
  • codeguy123
    2 years
    # text 0.70 KB | 0 0
    1. Replace with this code:
    2.  
    3. const addLinks = (searchPhrase, hyperlink) => {
    4. if (!searchPhrase) {
    5. return;
    6. }
    7.  
    8. const document = DocumentApp.getActiveDocument();
    9. const body = document.getBody();
    10. let search = null;
    11. let count = 0;
    12.  
    13. while (count < 1) {
    14. search = body.findText(searchPhrase, search ? search : null);
    15. if (!search) {
    16. break;
    17. }
    18. const searchElement = search.getElement();
    19. const startIndex = search.getStartOffset();
    20. const endIndex = search.getEndOffsetInclusive();
    21. searchElement.asText().setLinkUrl(startIndex, endIndex, hyperlink);
    22. count++;
    23. }
    24.  
    25. document.saveAndClose();
    26. };
    27.  
  • justnathan6
    221 days
    # text 0.30 KB | 0 0
    1. So guys?? Which one should I use? I'm new and curious.
    2.  
    3. 1. To Dazentaki : please explain or answer the question.
    4. 2. To Agimenez : which code that doesn't work?
    5. 3. To Codeguy123 : Your code replace which code?
    6.  
    7. 4. Do you mean that those three have their own specific functions?
    8.  
    9. Thank you before.
Add Comment
Please, Sign In to add comment
Advertisement