google-apps-script - 谷歌应用脚本在谷歌幻灯片中创建链接
问题描述
我正在从 Google 表格中提取数据以从模板创建新的 Google 幻灯片。一切正常,但是一旦我用电子表格数据替换了模板变量,我就无法弄清楚如何将文本作为链接点击。
如何用网站链接替换我的网站变量 company_website 并使其可点击?
function createSlidesfromSheets() {
var dataSpreadsheetUrl = "https://docs.google.com/spreadsheets/d/xxxxxlink/edit"; //make sure this includes the '/edit at the end
var ss = SpreadsheetApp.openByUrl(dataSpreadsheetUrl);
var deck = SlidesApp.getActivePresentation();
var sheet = ss.getSheetByName('Sheet1'); // this needs to be the name of the sheet/feuille
var values = sheet.getRange('A2:M20').getValues(); //this is the range of data to create the slides from.
//Logger.log(values);
var slides = deck.getSlides();
var templateSlideOne = slides[0];
var templateSlideTwo = slides[1];
var presLength = slides.length;
values.forEach(function(page){
if(page[0]){
var company_name = page[0];
var company_country = page[1];
var company_city = page[2];
var company_website = page[3];
var company_description = page[4];
templateSlideOne.duplicate(); //duplicate the first template slide
templateSlideTwo.duplicate(); //duplicate the second template slide
slides = deck.getSlides(); //update the slides array for indexes and length
newSlideOne = slides[1]; // declare the copy to update of the first template slide (right after the first template's slide)
newSlideTwo = slides[3]; // declare the copy to update of the second template slide (right after the second template's slide)
var firstShapes = (newSlideOne.getShapes());
firstShapes.forEach(function(shape){
shape.getText().replaceAllText('{{company-name}}',company_name);
shape.getText().replaceAllText('{{company-city}}',company_city);
shape.getText().replaceAllText('{{company-country}}',company_country);
shape.getText().replaceAllText('{{company-website}}',company_website);
// how do I use setLinkUrl() to make the new company_website text a clickable link?
});
var secondShapes = (newSlideTwo.getShapes());
secondShapes.forEach(function(shape){
shape.getText().replaceAllText('{{company-name}}',company_name);
shape.getText().replaceAllText('{{company-description}}',company_description);
});
presLength = slides.length;
newSlideOne.move(presLength);
presLength = slides.length;
newSlideTwo.move(presLength);
} // end our conditional statement
}); //close our loop of values
}
解决方案
我相信你的目标如下。
- 您想
shape.getText().replaceAllText('{{company-website}}',company_website)
在脚本中提供超链接。
为了实现这一点,这个修改怎么样?
从:
firstShapes.forEach(function(shape){
shape.getText().replaceAllText('{{company-name}}',company_name);
shape.getText().replaceAllText('{{company-city}}',company_city);
shape.getText().replaceAllText('{{company-country}}',company_country);
shape.getText().replaceAllText('{{company-website}}',company_website);
// how do I use setLinkUrl() to make the new company_website text a clickable link?
});
到:
firstShapes.forEach(function(shape){
var text = shape.getText();
text.replaceAllText('{{company-name}}',company_name);
text.replaceAllText('{{company-city}}',company_city);
text.replaceAllText('{{company-country}}',company_country);
var n = text.replaceAllText('{{company-website}}',company_website);
if (n > 0) text.getTextStyle().setLinkUrl(company_website);
});
- 在此修改中,TextStyle 是从 TextRange 中检索并
setLinkUrl
使用的。此时,from的返回值replaceAllText
作为replace的校验。
参考:
补充:回答第二个问题
关于你的新问题Does setLinkUrl(startOffset, endOffsetInclusive, url) not work in this context?
,我认为幻灯片服务中没有方法setLinkUrl(startOffset, endOffsetInclusive, url)
。我认为这可能是文档服务的方法。Ref所以这不能直接使用。如果您想将链接反映到文本部分,请使用
示例脚本:
firstShapes.forEach(function(shape){
var text = shape.getText();
text.replaceAllText('{{company-name}}',company_name);
text.replaceAllText('{{company-city}}',company_city);
text.replaceAllText('{{company-country}}',company_country);
var n = text.replaceAllText('{{company-website}}',company_website);
if (n > 0) text.getRange(startOffset, endOffset).getTextStyle().setLinkUrl(company_website); // Modified
});
- 在这种情况下,请设置
startOffset, endOffset
.
参考:
推荐阅读
- sql-server - 为什么 COUNT() 与 MS SQL Server 中的 SELECT TOP 一起使用或不使用?
- r - 使用R加载多个RDS文件保存在同一目录中
- mule-esb - Mule ESB 内核 4.2 - wrapper.conf java.additional proeprties 中的空白
- node.js - 如何解决“用户验证失败:密码:需要路径‘密码’。电子邮件:路径‘电子邮件’是必需的。名称:路径‘名称’是必需的。”
- c# - SMTP 未在生产中发送电子邮件 (IIS)
- reactjs - 浏览器如何记住插入空格而不在标记中显示
- .net - 使用 C# NEST 客户端时的 Elasticsearch 代码注入
- css - Angular Material matTooltip 折线元素
- css - 在 Reactjs 中使用 CSS className 样式化 react-select 组件
- c++ - C++ 打印 chrono::duration 的天数、小时数、分钟数等