简体   繁体   English

Google表格自定义脚本

[英]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. 我想通过Google Apps脚本构建自定义的Google表格功能,但是我一直陷于困境。

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. 我想从第一列和第二列(A1,A2 ... B1,B2等)中获取文本,并使用自定义文本返回它们,特别是将它们包装在标签中。

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 . 因此,如果在A1中有“ Can of Soda”,而在A2中有“ Can of Soda”,我希望函数返回最后一个字符串“ Can of Soda”和“ $ 0.99”,但将它们分别包装到span标记中,然后串联在一起字符串

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). 当我查看Google脚本日志时,可以将每列中的项目作为单独的日志返回(尽管第一列不返回结束标记)。 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. 当我希望for循环遍历每一行时,我也只浏览第一行。

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: 如果您确实想使用自定义功能执行此操作,请在C1中输入:

=weeklyMenu(A1,B1) = 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): 您也可以只使用一个公式(再次输入C1):

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

Either one can be copied down. 任一个都可以复制下来。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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