Iterate through every item in Google Docs and duplicate them onto a fresh page

I am currently developing a script that allows teachers to easily create translations of documents stored on Google Drive. The script is expected to go through the body elements of the document, translate the text, and paste it onto a new page appended to the document. It should also be able to identify images and include them in the translated document.

While I have successfully implemented text translation - where paragraph text is translated and copied to a new page - I am facing issues with copying images from the document.

Script

function translate() {
  var doc = DocumentApp.getActiveDocument();
  var body = doc.getBody();

  body.appendPageBreak();

  var elements = body.getNumChildren();
  for( var i=0;i<elements;i++) {
   var element = body.getChild(i).copy();
    var type = element.getType();
    if( type == DocumentApp.ElementType.PARAGRAPH ){
      var text = element.asParagraph().getText();
      var spn = LanguageApp.translate(text, 'en', 'es');
      body.appendParagraph(spn);
     }
    else if( type == DocumentApp.ElementType.INLINE_IMAGE ){
      var img = element.asInlineImage().getBlob();
      body.appendImage(img);
    }
  }
}

During my research, I came across this Stack Overflow post which helped me print the elements of the document in the console, aiding my logic. The test script in the post includes a paragraph with an INLINE_IMAGE, but my script is not successfully copying it to the new page. The text, however, is being translated correctly.

Do you think I need to implement a different approach in the logic to handle images along with text?

Answer №1

In reply to your query, if the image you are looking for is placed within the second paragraph, you can retrieve it by first accessing the paragraph and then locating the image within it. You can refer to my response in this post for a more detailed explanation.

I trust that this information will be beneficial to you!

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Prevent automatic scrolling to the top of the page after submitting a form. Remain at the current position on the

After submitting a form, I want to prevent the page from automatically scrolling back up. How can I keep the same position on the page after form submission? <script type="text/javascript"> var frm = $('#form'); frm.submit(function ...

Issue encountered when attempting to execute a JavaScript AppleScript from another JavaScript AppleScript due to permissions error

I am in the process of organizing my .applescript files by separating them into different ones for better organization. Within my JS AppleScript file named Test.applescript, I am attempting to execute another JS AppleScript file called Group Tracks Depend ...

Is jQuery capable of appropriately escaping my quotes?

Currently, I am utilizing $.cookie() to retrieve all the values from a cookie which are stored in JSON format: var properties = $.cookie('params'); The output of properties is: {"distinct_id": "13f97d6600b42e-000e6293c-6b1b2e75-232800-13f97d66 ...

Transmit information to PHP script using JavaScript

I am facing an issue with sending form data to another PHP file. While the process is working, the problem lies in the fact that once the data is submitted, the page does not redirect to the specific page as intended. It seems like it doesn't work th ...

Storing the results of an Ajax call in a global variable

What is the best way to store and access the output of an ajax call within a global variable? let globalOutput = []; $.ajax({ type: "GET", url: uri, dataType : "json", contentType: "application/json", data: { input: filterVa ...

The JSON data script is not functioning properly

Is this JSON formatted correctly? Why is it not displaying in the element with #id? I found a similar code snippet on https://www.sitepoint.com/colors-json-example/, copied and replaced the values but it's not functioning. Can anyone shed some light o ...

Manipulate HTML elements using JavaScript when a key is being held down

I'm currently developing a simple game using vueJS for the frontend. I have successfully created all the necessary objects and now my goal is to enable their movement when a key is pressed. However, I am facing an issue where the object only moves on ...

Uniform Height for Several Selectors

I came across a script on Codepen created by RogerHN and decided to customize it for a project I'm currently working on: https://codepen.io/RogerHN/pen/YNrpVa The modification I made involved changing the selector: var matchHeight = function ...

Delete JSON row columns efficiently without the need for iteration

Looking for a way to filter out specific data from your JSON object without looping through the entire thing? Here is a sample of the JSON data: [ { "cost":"KES 0.8000", "messageId":"ATXid_0fae395279b54d51519de5581230a7e ...

Issues with the "noImplicitAny" setting in TS compilation

Having the following code snippet: let z; z = 50; z = 'z'; paired with the configuration in my tsconfig.json file: { "compilerOptions": { "target": "es5", "module": "commonjs", "sourceMap": false, "noEmitOnError": true, " ...

The project is not being recognized by 'webpack' when running within it

Every time I attempt to execute 'webpack' in my project, the command line shows me this error message: 'webpack' is not recognized as an internal or external command, operable program or batch file. I have installed webpack using th ...

Retrieve a string value in Next.JS without using quotation marks

Using .send rather than .json solved the problem, thank you I have an API in next.js and I need a response without Quote Marks. Currently, the response in the browser includes "value", but I only want value. This is my current endpoint: export ...

What is the method to retrieve a value from a function call when a button is pressed?

Exploring the world of JavaScript, I'm currently working on a small program in React Native. My goal is to create a function SampleFunction2 that returns census data, and then render it on a FlatList when a button is pressed. Am I missing something by ...

Transferring specific form data through a POST request

I am attempting to transfer specific values from a form to PayPal once the form is submitted. Below is the code for my form: <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form class="form-horizo ...

I am constantly reminded by React hooks to include all dependencies

Imagine I am using useEffect to pre-fetch data upon initial rendering: function myComponent(props) { const { fetchSomething } = props; ... ... useEffect(() => { fetchSomething(); }, []); ... ... } My linter is warni ...

swapping the final word in a string with Node.js or JavaScript

var str = "Demo Docs Version 1.0.1"; var gotWord = str.split(" ").splice(-1)[0] str = str.replace(gotWord, "testing"); console.log(str); If there is a space between words, I can replace the last word. But how do I replace the last word when ...

Create a div element within the parent window of the iFrame

I'm trying to figure out how I can click a button within an iFrame that contains the following code: <td class="id-center"> <div class="bs-example"> <a id="comments" href="comments.php?id=$id" name="commen ...

Certain images are not being retrieved by Express on Linux, although they are functioning properly on Windows

When using a template to display HTML code, everything works smoothly on Windows. However, when transferring the code to a Linux machine, an issue arises - "Cannot GET /SmallVacImages/1.jpg". It seems that the index.html file can load images from the publi ...

Tips for incorporating a Forgot/Reset password option into your #Firebase platform

In the project I'm working on, I am utilizing #AngularFire2. My goal is to incorporate a Reset / Forgot password link into the login page. Does anyone have suggestions on how to accomplish this task? I'm looking to get some insights from #AskFi ...

The jQuery Ajax Error is consistently being triggered

I'm puzzled as to why my custom callback error function keeps getting triggered. When I remove this callback function, the success callback works just fine. Some sources online suggest that it could be an encoding issue, but I don't think that&a ...