简体   繁体   中英

Google Sheets Custom Script

I want to build a custom Google Sheets function via Google Apps Script and I've gotten stuck along the way.

I want to take the text from the first and second column (A1, A2 ...B1, B2 etc..) and return them with custom text, specifically to have them wrapped in tags.

So if in A1 we have "Can of Soda" and in A2 "$0.99", I would like function to return a final string of "Can of Soda " and "$0.99", but individually wrapped into span tags and then concatenated together into one string .

When I look at the Google Scripts log, I can return the the items in each columns as separate logs (although the first column doesn't return the closing tag). The question is how I get them into one string. I also only go through the first row, when I would like that for loop to go through each row.

Here is my log:

[16-05-30 11:32:02:348 EDT] <span>Can of Soda [16-05-30 11:32:02:349 EDT] <span>0.99</span>

Here's my code as of now:

function weeklyMenu() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var data = sheet.getDataRange().getValues();
  for (var i = 0; i < data.length; i++) {
   product = Logger.log("<span>" + data[i][0]) + "</span> ";
   price = Logger.log("<span>" + data[i][1] + "</span>");
  }
} 

\\\\The end goal is to have a string that says "<span>Can of Soda</span> <span>$0.99</span>"

If you really want to do this with a custom function, in C1 enter:

=weeklyMenu(A1,B1)

The code is:

function weeklyMenu(A,B) {
 var sheet = SpreadsheetApp.getActiveSheet();
 var con="<span>"+A+"</span> <span>"+B+"</span>";
 return con
 }

You could also just use a formula (again enter in C1):

="<span>"&A1&"</span> <span>"&B1&"</span>"

Either one can be copied down.

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