0

Hi I am trying to read values from created textboxes in Google Slides

So I am creating multiple textboxes by calling this function for each value in arrays

function newTextBox(textBoxID, xpos, ypos, textBoxText) {

var elementId = textBoxID;
var requests = [{
  createShape: {
    objectId: elementId,
    shapeType: 'TEXT_BOX',
    elementProperties: {
      pageObjectId: pageId,   
      'size': {
        'width': {
          'magnitude': 53.5748,
          'unit': 'PT'
        },
        'height': {
          'magnitude': 22.6772,
          'unit': 'PT'
        }

      },
      transform: {
        scaleX: 1,
        scaleY: 1,
        translateX: xpos,
        translateY: ypos,
        unit: 'PT'
      }
    }
  }
},

// Insert text into the box, using the supplied element ID.
{
  insertText: {
    objectId: elementId,
    insertionIndex: 0,
    text: textBoxText
  }
}];

// Execute the request.
var createTextboxWithTextResponse = Slides.Presentations.batchUpdate({
  requests: requests
}, presentationId);
var createShapeResponse = createTextboxWithTextResponse.replies[0].createShape;
console.log('Created textbox with ID: %s', createShapeResponse.objectId);
}

So the textBoxID are IDs I defined for easy reference later. The textBoxText is initially set as 0.

The purpose of this script is to compare each value in the textboxes, with a value in an array that I have. Calculations are then done, and the value is then updated. I've seen many similar questions, but can't seem to find the right answer.

I don't want to just update the value, I specifically need to read the value. What I basically need is something like textBoxID[1].getText(), but I can't find something similar/ something that achieves the same goal. Any help would be greatly accepted

Rubén
  • 34,714
  • 9
  • 70
  • 166

1 Answers1

1
  • When the textbox of textBoxID is not existing, you want to create new textbox.
  • When the textbox of textBoxID is existing, you want to modify the value in the textbox.
  • About creating the textbox, you want to use the function of newTextBox.
  • You want to achieve this using Google Apps Script.
  • You have already been able to create new textbox using your script.

If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.

Sample situation:

In this modification, as a sample situation, the initial value of textbox is 0. And when the textbox is not existing in the slide, new textbox is created. When the textbox is existing in the slide, the value of textbox is modified. In this case, 1 is added to the existing value.

Pattern 1:

In this pattern, your script of newTextBox is used. Before you use this, please set the variables of presentationId, pageId and data. data is a sample value.

Sample script:

// Please set following variables.
const presentationId = "###";
const pageId = "###";

function newTextBox(textBoxID, xpos, ypos, textBoxText) {
  var elementId = textBoxID;
  var requests = [
        {createShape: {objectId: textBoxID, shapeType: 'TEXT_BOX', elementProperties: {pageObjectId: pageId, size: {width: {magnitude: 53.5748, unit: 'PT'}, height: {magnitude: 22.6772, unit: 'PT'}}, transform: {scaleX: 1, scaleY: 1, translateX: xpos, translateY: ypos, unit: 'PT'}}}},
        {insertText: {objectId: textBoxID, insertionIndex: 0, text: textBoxText}}
  ];
  var createTextboxWithTextResponse = Slides.Presentations.batchUpdate({requests: requests}, presentationId);
  var createShapeResponse = createTextboxWithTextResponse.replies[0].createShape;

  Logger.log('Created textbox with ID: %s', createShapeResponse.objectId);
}

// Please run this function.
function myFunction() {
  var data = [
    {textBoxID: "###", xpos: ###, ypos: ###, textBoxText: "0"},
    {textBoxID: "###", xpos: ###, ypos: ###, textBoxText: "0"},
    {textBoxID: "###", xpos: ###, ypos: ###, textBoxText: "0"},
  ];


  const slide = SlidesApp.openById(presentationId).getSlideById(pageId);
  const obj = slide.getShapes().reduce((o, e) => {
    o[e.getObjectId()] = e;
    return o;
  }, {});
  ar.forEach(({textBoxID, xpos, ypos, textBoxText}) => {
    if (textBoxID in obj) {
      const oldValue = obj[textBoxID].getText().asString(); // Here, you can retrieve the value of existing textbox.
      const newValue = Number(oldValue) + 1;
      obj[textBoxID].getText().replaceAllText(oldValue, newValue);
    } else {
      newTextBox(textBoxID, xpos, ypos, textBoxText);
    }
  });
}

Pattern 2:

In this pattern, one request body is created using the values of data and use it to the method of batchUpdate. By this, only one API call is used. Before you use this, please set the variables of presentationId, pageId and data. data is a sample value.

Sample script:

function myFunction() {
  // Please set following variables.
  var data = [
    {textBoxID: "###", xpos: ###, ypos: ###, textBoxText: "0"},
    {textBoxID: "###", xpos: ###, ypos: ###, textBoxText: "0"},
    {textBoxID: "###", xpos: ###, ypos: ###, textBoxText: "0"},
  ];
  const presentationId = "###";
  const pageId = "###";


  const slide = SlidesApp.openById(presentationId).getSlideById(pageId);
  const obj = slide.getShapes().reduce((o, e) => {
    o[e.getObjectId()] = e;
    return o;
  }, {});
  const requests = data.reduce((ar, {textBoxID, xpos, ypos, textBoxText}) => {
    if (textBoxID in obj) {
      const oldValue = obj[textBoxID].getText().asString(); // Here, you can retrieve the value of existing textbox.
      const newValue = Number(oldValue) + 1;
      obj[textBoxID].getText().replaceAllText(oldValue, newValue);
    } else {
      ar.push([
        {createShape: {objectId: textBoxID, shapeType: 'TEXT_BOX', elementProperties: {pageObjectId: pageId, size: {width: {magnitude: 53.5748, unit: 'PT'}, height: {magnitude: 22.6772, unit: 'PT'}}, transform: {scaleX: 1, scaleY: 1, translateX: xpos, translateY: ypos, unit: 'PT'}}}},
        {insertText: {objectId: textBoxID, insertionIndex: 0, text: textBoxText}}
      ]);
    }
    return ar;
  }, []);
  if (requests.length > 0) {
    var createTextboxWithTextResponse = Slides.Presentations.batchUpdate({requests: requests}, presentationId);
    Logger.log(createTextboxWithTextResponse)
  }
}

Note:

  • In your script, console.log is used. So I thought that your script editor might be using with enabling V8 runtime. So I modified the script as V8. Please be careful this.
  • This is a simple modified script. So please modify this for your actual situation.

References:

If I misunderstood your question and this was not the direction you want, I apologize.

Tanaike
  • 181,128
  • 11
  • 97
  • 165