Gerry St.Pierre
Gerry St.Pierre

Reputation: 201

How can I create superscript/subscript characters in a document?

I'm working on a script to convert some documents in a proprietary format over to google docs. I've been able to handle most of the various formatting options (fonts, point sizes, bold, etc.) but I'm stuck on subscript and superscript.

I've tried creating a new google doc with a paragraph in subscript. I then wrote a script to look at the paragraphs and children and in both cases didn't see an attribute that was set as I could for underline.

I've tried wrapping the text in st but that only added that exact text to the document. Here is a bit of the code I'm currently using:

if (superscript == true ) {
  paragraph.appendText(txt.sup()).setAttributes(style);
} else if (subscript == true) {
  paragraph.appendText(txt.sub()).setAttributes(style);
} else {
  paragraph.appendText(txt).setAttributes(style);
}

Anything else to try?

Upvotes: 1

Views: 3064

Answers (3)

Tanaike
Tanaike

Reputation: 201553

  • You want to put the superscript and subscript characters to Google Document using Google Apps Script.

If my understanding is correct, how about this sample script? Unfortunately, even in the current stage, there are still no methods for putting the superscript and subscript characters using Document Service.

But when Google Docs API which was added at early 2019 is used, those can be achieved. Such function might be added to Document Service in the future. So as a current workaround, I would like to propose the method using Docs API. The sample script is as follows.

Before you use this script, please enable Google Docs API at Advanced Google services.

Sample script:

function myFunction() {
  var documentId = "###"; // Please set document ID here.

  var resource = {requests: [
    {insertText: {text: "SampleSuperscriptSubscript", location: {index: 1}}},
    {updateTextStyle: {range: {startIndex: 7, endIndex: 18}, textStyle: {baselineOffset: "SUPERSCRIPT"}, fields: "baselineOffset"}},
    {updateTextStyle: {range: {startIndex: 18, endIndex: 27}, textStyle: {baselineOffset: "SUBSCRIPT"}, fields: "baselineOffset"}}
  ]};
  Docs.Documents.batchUpdate(resource, documentId);
}

Result:

When above script is run for new Google Document, the following result is obtained. At first, a text of SampleSuperscriptSubscript is put. Then, the text style is modified. These are run using the batchUpdate method. If you want to put those values for the existing Document, please modify above object of resource.

enter image description here

References:

If this was not the direction you want, I apologize.

Upvotes: 1

Louis Maddox
Louis Maddox

Reputation: 5576

For the benefit of anyone looking for this, it's been implemented since this question was asked as a TextAlignment class - it's not explicit in the documentation*, but thankfully these create indices on text elements observed with the getTextAttributeIndices method.

* as in, it's not an attribute listed by getAttributes but is indexed with getTextAttributeIndices()

I've just modified a Docs-to-markdown processing script I'm working on to incorporate sub- and superscript alignment handling which may be useful as an example (see commit diff).

Note that if applied across paragraphs the getTextAlignment method returns null.

Upvotes: 1

gjuggler
gjuggler

Reputation: 511

There's currently no way to do this — it would exist somewhere in the Text class API (https://developers.google.com/apps-script/reference/document/text) if it were possible.

I filed a feature request for superscript / subscript here https://code.google.com/p/google-apps-script-issues/issues/detail?id=2885 . Feel free to star the issue to register your support.

Upvotes: 2

Related Questions