Georgie L
Georgie L

Reputation: 31

Count instances of text string and replace with number

I am trying to find ways to speed up adding footnotes to a Google slides document. What I want is a script that looks for every instance of a text string throughout the document (say ‘*’) and then replaces each instance of that string with the number corresponding to that instance e.g. the first * gets replaced with 1, second * gets replaced with 2, and so on. I realise this method can only be used once but this would still save me a lot of time. Is there an easy way to do this? I can’t work out how to replace with a variable but it seems like it should be possible.

Upvotes: 1

Views: 513

Answers (2)

NightEye
NightEye

Reputation: 11214

Assuming that we have this slide below as our sample data.

Sample Data:

sample1 sample2

If we want to replace all occurrences of a string (e.g. "replace"), then we will need to traverse all shapes of each slides and replace its occurrences with the counter.

Code:

function myFunction() {
  var presentation = SlidesApp.getActivePresentation();
  var slides = presentation.getSlides();
  var counter = 0;
  // traverse each slide
  slides.forEach(function (slide) {
    var shapes = slide.getShapes();
    // traverse each shape
    shapes.forEach(function (shape) {
      // get its text content
      var text = shape.getText()
      var string = text.asString();
      // replace all occurrences of string (e.g. "replace")
      // by an incrementing number
      string = string.replace(/replace/g, function() {
        return ++counter;
      });
      // set the shape's text
      text.setText(string);
    });
  });
}

Output:

output1 output2

Upvotes: 1

Yuri Khristich
Yuri Khristich

Reputation: 14537

Not exactly a ready solution. Rather the way to solve the task.

You can download all texts of your presentation as a TXT file:

enter image description here

Then you can process this text with JS script. Something like this:

// your text with markers (#)
var txt = `
doleste # atus etur, consequi odi quos alit audipsunt as is est# ant.

consequi # odi quos alit audipsunt es vere ipsam aut am

doluptae et que nonse # um volupta aped ulloreictat as is est ant.
`;

// get every marker + several characters before and after
var find_for = txt.match(/...#.../g);

console.log(find_for); // Output: [ 'te # at', 'est# an', 'ui # od', 'se # um' ]

// replace marker with numbers 1, 2, 3...
var replace_with = find_for.map((m,i) => m.replace(/#/, i+1));

console.log(replace_with); // Output: [ 'te 1 at', 'est2 an', 'ui 3 od', 'se 4 um' ]

This way you will get two arrays: find_for and replace_with.

Then you will need a script to perform the text replaces.

'te # at' --> 'te 1 at'
'est# an' --> 'est2 an'
'ui # od' --> 'ui 3 od'
'se # um' --> 'se 4 um'

Which is, I believe, a quite trivial task.

But there can be errors if some markets has the same neighbor characters. Probably you need to take four or five neighbor characters with markers: ....#.... or asymetric ......#... It's up to you.

Upvotes: 0

Related Questions