简体   繁体   中英

How to add a variable to the hyperlink function using Google Apps Script

I am using Google Apps Script to build custom reports with Google Sheets. One of my columns is a column of numbers and I want to make each number a hyperlink. The link I need to use for each number is almost the same, with the only difference being the number in the cell itself. That is the last part that needs to be included in the link. I am not sure how to do this using the hyperlink formula.

The whole hyperlink formula is surrounded in quotes and I am not sure how to move the quotes around so that it accepts the variable I am trying to include.

function hyperlinkTest() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheets()[0];
  var cell = sheet.getRange("B5");
  cell.setFormula('=HYPERLINK("http://www.google.com/","Google")');
}

The above code is how to include a hyperlink in Google sheets, but the formula needs to be edited in a way where I can insert a variable. At the end of 'google.com' for example. When I try to do this it is just a string, I need it to recognize the variable.

您需要做的就是转义单引号并使用+添加变量,再使用单引号来完成公式的其余部分。

cell.setFormula('=HYPERLINK("http://www.google.com/'+variablehere+'","Google")');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM