0

This question was cross-posted to Web Applications: Help me find my Element in a Google Doc so I can act on it in script?

Here is my Google Doc: https://docs.google.com/document/d/1TgzOIq0g4DyDefmJIEnqycDITIx_2GNQkpdxMGwtqB8/edit?usp=sharing

You can see a button in it titled ENTER NEW NOTE.

I have been successful at rolling through the elements of the doc to find the table and to replace txt in those areas as needed. But the button here needs to have the URL changed, and I cannot figure out how to do it.

This URL seems to give an idea, but I cannot turn it into my answer since I don't quite understand. Mail merge: can't append images from template

Would someone help me with this to the point of showing the actual code, because I have tried to edit the many examples found about elements, setURL and looking to parent, etc. I just end up with a mess.

I am calling the script from a Google Sheet, to wok on a BUNCH of Google Docs. (I will be running through the spreadsheet to get URL's for the next doc to have it's URL replaced.

Here is as close as I believe I have gotten:

function getDocElements() {
  var doc = DocumentApp.openByUrl("https://docs.google.com/document/d/1TgzOIq0g4DyDefmJIEnqycDITIx_2GNQkpdxMGwtqB8/edit?usp=sharing"),
      body = doc.getBody(),
      numElements = doc.getNumChildren(),
      elements = [];

  for (var i = 0; i < numElements; ++i){
      var element = doc.getChild(i),
          type = element.getType();
//          daURL = element.getURL();

    // Look for child elements within the paragraph. Inline Drawings are children.
//  if(element.asParagraph().getNumChildren() !=0 && element.asParagraph().getChild(0).getType() == DocumentApp.ElementType.INLINE_DRAWING) {

    var drawingRange = body.findElement(DocumentApp.ElementType.INLINE_DRAWING);
           while (drawingRange != null) {
               var element = drawingRange.getElement();
               var drawingElement = element.asInlineDrawing();
               //drawingElement.removeFromParent();
             drawingElement.setURL("http://www.google.com");
               drawingRange = body.findElement(DocumentApp.ElementType.INLINE_DRAWING);
           }


    // For whatever reason, drawings don't have their own methods in the InlineDrawing class. This bit copies and adds it to the bottom of the doc.
    //var drawing = element.asParagraph().copy();
    //body.appendParagraph(drawing);

  }


    Logger.log(i + " : "+type);
}

Here is my newest iteration that shows in the logs the elements, including the inLineDrawing I want to change...

===========

function getDocElement() {
  var doc = DocumentApp.openByUrl("https://docs.google.com/document/d/1TgzOIq0g4DyDefmJIEnqycDITIx_2GNQkpdxMGwtqB8/edit?usp=sharing"),
      body = doc.getBody(),
      numElements = doc.getNumChildren(),
      elements = [];

  for (var i = 0; i < numElements; ++i){
      var element = doc.getChild(i),
          type = element.getType();
//        daURL = element.getURL();

     Logger.log(i + " : " + numElements + " : "+ type + " " + element);


    // Search through the page elements. Paragraphs are top-level, which is why I start with those.
if( type == DocumentApp.ElementType.PARAGRAPH ){

  // Look for child elements within the paragraph. Inline Drawings are children.
  if(element.asParagraph().getNumChildren() !=0 && element.asParagraph().getChild(0).getType() == DocumentApp.ElementType.INLINE_DRAWING) {

    //element.getParent().setLinkUrl("http://www.google.com");
    Logger.log(element.asParagraph().getChild(0).getType() + " : " + element.getAttributes());

    // For whatever reason, drawings don't have their own methods in the InlineDrawing class. This bit copies and adds it to the bottom of the doc.
    var drawing = element.asParagraph().copy();
    //body.appendParagraph(drawing);

//    body.appendParagraph();
    if(element.getParent() !=''){
       //element.asParagraph().appendHorizontalRule();
      //element.editAsText().appendText("text");
     // element.getParent().insertHorizontalRule(0);

    }


  }
}


  }

}
Community
  • 1
  • 1
Jim Garner
  • 43
  • 1
  • 9

2 Answers2

2

I'm not sure why the setLinkUrl() is not available for InlineDrawing

If you can replace your drawing with an image (You can download your drawing as png or svg and insert it), you will be able to use setLinkUrl

Here is an example:

function myFunction() {

 var body = DocumentApp.getActiveDocument().getBody();

 // All inline images as a RangeElement
 var images = body.findElement(DocumentApp.ElementType.INLINE_IMAGE);

 // select first image, in case your doc has more than one you'll need to loop
 var element = images.getElement();
 var image = element.asInlineImage();
 image.setLinkUrl("www.google.com");
}
ocordova
  • 1,788
  • 2
  • 13
  • 20
  • I think you are showing me how to set the URL for an image, and suggesting that I replace the Drawing with an Image to use this. I don't know how I would do that. Also, this is the test because I have 600+ folders with these docs in them that must be done, so scripting to find the solution is practically a must. – Jim Garner Oct 18 '16 at 01:14
  • Please @ocordova, tell me how to replace the drawings with images from a sheet list of document urls? This is the only suggestion that might work. – Jim Garner Oct 19 '16 at 03:04
  • I am actually surprised no one is able to respond to this with deep expertise. Is inlinedrawings so infrequently used? Is it not a healthy process for building buttons this way, which is suggested around the internet? – Jim Garner Oct 19 '16 at 03:05
  • Sadly [InlineDrawing](https://developers.google.com/apps-script/reference/document/inline-drawing) currently doesn't have the setLinkUrl method. I suggested to changing the `inlineDrawings` with `inlineImages`, thinking it was your first approach, but if I understand you correctly you already have 600+ documents with an inlineDrawing as a button? – ocordova Oct 20 '16 at 16:37
  • Correct. Already have over 600 folders with a doc in each. All formatted the same way, so the element I need to change the URL on, should be in the same position on each one. – Jim Garner Oct 22 '16 at 22:04
  • I have added the latest script to the above question, still hoping for help. Now I have been able to get the script to show me the elements... even the inLineDrawing. But I am still unable to change the URL of it, or do anything to it. – Jim Garner Oct 23 '16 at 19:32
0

Unfortunately the Class InlineDrawing doesn't have methods to access the attached links nor any other to programmatically change it to a InlineImage1. It looks to me that you will have have to make the link changes manually.

Related Feature requests:

  • Issue 3367: Allow exporting InlineDrawing as an image
  • Issue 1054: Add ability to create and modify drawings

References

1: Answer by Henrique Abreu to Modifying a drawing using Google Apps Script

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Rubén
  • 34,714
  • 9
  • 70
  • 166