Google App脚本可在Google幻灯片中创建链接
我正在从Google表格中提取数据,以便从模板中创建新的Google幻灯片.一切正常,但是一旦我用电子表格数据替换了模板变量,我就无法弄清楚如何使文本可作为链接单击.
I'm pulling data from a Google Sheet to create new Google Slides from a template. Everything works fine, but I can't figure out how to make text clickable as a link once I've replaced the template variable with the spreadsheet data.
如何用网站链接替换我的网站变量company_website并使其可点击?
How do I replace my website variable, company_website, with the website link AND make it clickable?
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)
.
为了实现这一点,如何进行修改?
In order to achieve this, how about this modification?
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);
});
- 在此修改中,从TextRange检索TextStyle,并使用
setLinkUrl
.那时,从replaceAllText
返回的值用作替换检查. - In this modification, TextStyle is retrieved from TextRange, and
setLinkUrl
is used. At that time, the returned value fromreplaceAllText
is used as the check of replace.
关于您的新问题setLinkUrl(startOffset,endOffsetInclusive,url)在这种情况下不起作用?
,我认为没有 setLinkUrl(startOffset,endOffsetInclusive,url)的方法)
.我认为这可能是文档服务的方法.参考因此,这不能直接用过的.如果您想反映文本部分的链接,请使用
And about your new question of Does setLinkUrl(startOffset, endOffsetInclusive, url) not work in this context?
, I think that there is no method of setLinkUrl(startOffset, endOffsetInclusive, url)
in Slides service. I thought that this might be the method of Document service. Ref So this cannot be directly used. If you want to reflect the link to the part of text, please use
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
.