Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const addLinks = (searchPhrase, hyperlink) => {
- const document = DocumentApp.getActiveDocument();
- const body = document.getBody();
- let search = null;
- let count = 0;
- while (count < 1 && (search = body.findText(searchPhrase, search))) {
- const searchElement = search.getElement();
- const startIndex = search.getStartOffset();
- const endIndex = search.getEndOffsetInclusive();
- searchElement.asText().setLinkUrl(startIndex, endIndex, hyperlink);
- count++;
- }
- document.saveAndClose();
- };
- addLinks("Kiton", "https://2men.it/collections/kiton/");
Advertisement
Comments
-
- function addLinksFromSheet() {
- var sheet = SpreadsheetApp.openById("YOUR_SPREADSHEET_ID").getSheetByName("Sheet1");
- var data = sheet.getDataRange().getValues();
- var document = DocumentApp.getActiveDocument();
- var body = document.getBody();
- for (var i = 0; i < data.length; i++) {
- var searchPhrase = data[i][0];
- var hyperlink = data[i][1];
- var search = null;
- var count = 0;
- while (count < 1 && (search = body.findText(searchPhrase, search))) {
- var searchElement = search.getElement();
- var startIndex = search.getStartOffset();
- var endIndex = search.getEndOffsetInclusive();
- searchElement.asText().setLinkUrl(startIndex, endIndex, hyperlink);
- count++;
- }
- }
- document.saveAndClose();
- }
-
- 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.
-
- How does that work could you break it down for me?
- You add all your articles to one google sheet and then run the code?
-
- The code is not working, I don't know what I am missing, is giving me the error:
- Error
- Exception: Invalid argument: searchPattern
- addLinks @ Code.gs:6
- Any idea of what I can do to fix it?
- Thank you!
-
- Replace with this code:
- const addLinks = (searchPhrase, hyperlink) => {
- if (!searchPhrase) {
- return;
- }
- const document = DocumentApp.getActiveDocument();
- const body = document.getBody();
- let search = null;
- let count = 0;
- while (count < 1) {
- search = body.findText(searchPhrase, search ? search : null);
- if (!search) {
- break;
- }
- const searchElement = search.getElement();
- const startIndex = search.getStartOffset();
- const endIndex = search.getEndOffsetInclusive();
- searchElement.asText().setLinkUrl(startIndex, endIndex, hyperlink);
- count++;
- }
- document.saveAndClose();
- };
-
- So guys?? Which one should I use? I'm new and curious.
- 1. To Dazentaki : please explain or answer the question.
- 2. To Agimenez : which code that doesn't work?
- 3. To Codeguy123 : Your code replace which code?
- 4. Do you mean that those three have their own specific functions?
- Thank you before.
Add Comment
Please, Sign In to add comment
Advertisement