How do you pull all numbers from text in google slides with an apps script?

43 Views Asked by At

I'm trying to make a block of code that colours numbers depending on the value. I know how to pull text but not numbers that can be affected with operations. I'm new so sorry if it's extremely easy!

  var slides = Presentation.getSlides();
  slide.getShapes().forEach(shape => {

  var text = shape.getText();
  Logger.log(text.asString());

  var highNum = text.find("10")
  var greenNum = // code that pulls all numbers in text and between 9.75 and 9
  var lowNum = // code that pulls all numbers in text and between 3.75 and 0

  highNum.forEach(s => {
    s.getTextStyle().setForegroundColor("#FFD700");
  
  greenNum.forEach(s => {
    s.getTextStyle().setForegroundColor("#00FF00");

  lowNum.forEach(s => {
    s.getTextStyle().setForegroundColor("#FF0000");

just need a way to do all that in the comments

1

There are 1 best solutions below

2
Cooper On

Extracting numbers from text:

function myfunk1() {
  let text = "1.0, 2.0 , 3.1, 4.2, 5.3, 6.4, 7.5, 8.6, 9.0, 9.5, 10";
  let m = text.match(/[0-9.]{1,}/g);
  let r1 = m.filter(e => e >= 0 && e <= 3.75);
  let r2 = m.filter(e => e >= 9.0 && e <= 9.75);
  Logger.log(r1.join(','))
  Logger.log(r2.join(','))
}

Execution log
6:24:18 PM  Notice  Execution started
6:24:19 PM  Info    1.0,2.0,3.1
6:24:19 PM  Info    9.0,9.5
6:24:20 PM  Notice  Execution completed