使用 google 应用程序脚本将数据从 google 工作表注入到 google 幻灯片期间将日期转换为 dd/mm/yyyy
Convert date to dd/mm/yyyy during injection of data from google sheets to google slides using google apps script
我创建了一些脚本,允许我使用存储在 Google Sheet 中的数据生成一组 Google 幻灯片(基于预格式化模板幻灯片)。
下面脚本中的 PreviousTarget
和 CurrentTarget
引用了 Google Sheet 中格式为日期单元格的单元格。定义的格式是 dd/mm/yyyy。当我 运行 脚本时,正确的日期被注入到 Google 幻灯片中,但是,它们看起来像这样:
而不是 dd/mm/yyyy 格式
Sun Jul 26 2020 00:00:00 GMT+0100 (British Summer Time)
我可以通过将 Google Sheet 中两个单元格的格式更改为纯文本字符串来解决此问题。但是有没有办法在脚本 运行 期间重新格式化日期?
有什么想法可以在注入 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();
} ```
尝试改变
var values = sheet.getRange('A3:R').getValues();
到
var values = sheet.getRange('A3:R').getDisplayValues();
看看是否有帮助?
关于 getDisplayValues 的更多信息。
我创建了一些脚本,允许我使用存储在 Google Sheet 中的数据生成一组 Google 幻灯片(基于预格式化模板幻灯片)。
下面脚本中的PreviousTarget
和 CurrentTarget
引用了 Google Sheet 中格式为日期单元格的单元格。定义的格式是 dd/mm/yyyy。当我 运行 脚本时,正确的日期被注入到 Google 幻灯片中,但是,它们看起来像这样:
Sun Jul 26 2020 00:00:00 GMT+0100 (British Summer Time)
我可以通过将 Google Sheet 中两个单元格的格式更改为纯文本字符串来解决此问题。但是有没有办法在脚本 运行 期间重新格式化日期?
有什么想法可以在注入 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();
} ```
尝试改变
var values = sheet.getRange('A3:R').getValues();
到
var values = sheet.getRange('A3:R').getDisplayValues();
看看是否有帮助?
关于 getDisplayValues 的更多信息。