【问题标题】:Google App Script to create link in Google Slides谷歌应用脚​​本在谷歌幻灯片中创建链接
【发布时间】:2025-12-06 17:25:01
【问题描述】:

我正在从 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

}

【问题讨论】:

    标签: google-apps-script google-sheets google-slides


    【解决方案1】:

    我相信你的目标如下。

    • 您想在脚本中提供指向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。此时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

    参考:

    【讨论】:

    • 太好了,谢谢你,田池。 setLinkUrl(startOffset, endOffsetInclusive, url) 在这种情况下不起作用吗?
    • @yobobos 感谢您的回复。首先,请问您我的回答是否可以解决您的问题?如果那没有解决你的问题,我必须道歉。关于您的新问题,我又添加了一个示例脚本。你能确认一下吗?