Can I inject conditional formatting from Google Sheets into Google Slides using Google Apps Script?

依然范特西╮ 提交于 2020-06-29 03:58:08

问题


I've created a script that allows me to use data stored in a Google Sheet to generate a set of Google Slides (based on a pre-formatted template slide).

RAGStatus in the script below references a cell within the Google Sheet that has conditional formatting. If the cell states "Red" then the cell is filled with red colour. When I run the script, the correct data is injected into the Google Slide, but appears as a text string without the colour formatting.

Is there a way to inject the data and the formatting directly into Google Slides?

function generateProjectReports() {
  var dataSpreadsheetUrl = ""; //removed spreadsheet link as it has sensitive information within
  var ss = SpreadsheetApp.openByUrl(dataSpreadsheetUrl);
  var deck = SlidesApp.getActivePresentation();
  var sheet = ss.getSheetByName('1 - Project Overview');
  var values = sheet.getRange('A3:R').getDisplayValues();
  var slides = deck.getSlides();
  var templateSlide = slides[1];
  var presLength = slides.length;
  
  values.forEach(function(page){
  if(page[1]){
    
   var Project = page[1];
   var Lead = page [4]
   var RagStatus = page[5];
   var Trend = page[6];
   var Objectives = page[9];
   var Summary = page[10];
   var PreviousTarget = page[11];
   var CurrentTarget = page[12];
   var Risk1 = page[14];
   var Risk2 = page[15];
   var Issue1 = page[16];
   var Issue2 = page[17];
    
    
   templateSlide.duplicate(); //duplicate the template page
   slides = deck.getSlides(); //update the slides array for indexes and length
   newSlide = slides[2]; // declare the new page to update
    
    
   var shapes = (newSlide.getShapes());
     shapes.forEach(function(shape){
       shape.getText().replaceAllText('{{Project}}',Project);
       shape.getText().replaceAllText('{{Lead}}',Lead);
       shape.getText().replaceAllText('{{Objectives}}',Objectives);
       shape.getText().replaceAllText('{{Summary}}',Summary);
       shape.getText().replaceAllText('{{PreviousTarget}}',PreviousTarget);
       shape.getText().replaceAllText('{{CurrentTarget}}',CurrentTarget);
       shape.getText().replaceAllText('{{RagStatus}}',RagStatus);
       shape.getText().replaceAllText('{{Trend}}',Trend);
       shape.getText().replaceAllText('{{Risk1}}',Risk1);
       shape.getText().replaceAllText('{{Risk2}}',Risk2);
       shape.getText().replaceAllText('{{Issue1}}',Issue1);
       shape.getText().replaceAllText('{{Issue2}}',Issue2);
    }); 
   presLength = slides.length; 
   newSlide.move(presLength); 
  } // end our conditional statement
  }); //close our loop of values

//Remove the template slide
templateSlide.remove();
  
}

来源:https://stackoverflow.com/questions/62550610/can-i-inject-conditional-formatting-from-google-sheets-into-google-slides-using

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!