提问者:小点点

在使用google apps脚本将数据从google工作表注入到google幻灯片期间,将日期转换为dd/mm/yyyy


我创建了一个脚本,允许我使用存储在Google表格中的数据来生成一组Google幻灯片(基于预先格式化的模板幻灯片)。

PreviousTargetCurrentTarget在下面的脚本中引用格式化为Google表格中日期单元格的单元格。定义的格式是dd/mm/yyyy。当我运行脚本时,正确的日期被注入到Google幻灯片中,但是,它们不是dd/mm/yyyy格式,而是如下所示:

Sun Jul 26 2020 00:00:00GMT0100(英国夏令时)

我可以通过将两个单元格的格式更改为Google表格中的纯文本字符串来解决此问题。但是有没有办法在脚本运行期间重新格式化日期?

有什么想法可以在注入Google幻灯片期间修复格式吗?

  var dataSpreadsheetUrl = ""; //sorry, I removed link to the sheet here as it contained sensitive information

  var ss = SpreadsheetApp.openByUrl(dataSpreadsheetUrl);
  var deck = SlidesApp.getActivePresentation();
  var sheet = ss.getSheetByName('1 - Project Overview');
  var values = sheet.getRange('A3:R').getValues();
  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();
  
} ``` 

共1个答案

匿名用户

试着改变

var values = sheet.getRange('A3:R').getValues();

var values = sheet.getRange('A3:R').getDisplayValues();

看看有没有帮助?

有关getDisplayValue的更多信息。