简体   繁体   中英

Insertion of number sign (#) into a Google Spreadsheet via GAS

I have encountered a problem when trying to insert a "#" sign into a cell using script (that is triggered to run when a submission comes from a form).

I have tried to do it this way:

somecell.setFormula('=HYPERLINK("http://www.some.link/some/'+var+'#1a2b3c","'+var+'")');

So the output should be:

=HYPERLINK("http://www.some.link/some/1234#1a2b3c","1234")

But it is always inserted as only (no # and the rest of link after the sign)

=HYPERLINK("http://www.some.link/some/1234","1234")

There are no errors reported during execution. How should I put the sign into the script so it will be inserted into the formula? I had tried using ...1234(backslash)#1a... but it does not work.

Edit How does the script work (in short): firstly, sheet receives a form submission with an URL as one of the entries. Then, the script cuts off part of the link (using .split('/') and then .pop() to get the last element of the array that .split created) and the variable extracted this was is used in the .setFormula seen above as var .

Also, I have tried again; backslash does not work - everything after (and including) # is deleted.

You need to include a backslash to escape the character:

function addlink() {
  var somecell = SpreadsheetApp.getActiveSpreadsheet().getRange('F2');
  var blah = "123";
  somecell.setFormula('=HYPERLINK("http://www.some.link/some/'+blah+'\#1a2b3c","'+blah+'")');
  //                                                                ^^^
}

I tried your example like this :

function test(){
  var sh = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var xx = '1234'
  sh.getRange('A2').setFormula('=HYPERLINK("http://www.some.link/some/'+xx+'#1a2b3c","'+xx+'")');
}

and I get the expected result

在此处输入图片说明

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