在Google Apps脚本中串联字符串 [英] Concatenating strings in Google Apps Script

查看:76
本文介绍了在Google Apps脚本中串联字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将 i j 表示的两个值都放入 getSheetByName 函数?

How do I get both values represented by i and j into the getSheetByName function?

免责声明:我是编码的新手,可能会问错问题。我的目标是创建一个简单的代码,该代码将通过循环工作表名称来自动删除工作表:第1周,第2周等。

Disclaimer: I am brand new at coding and am probably asking the wrong questions. My goal is to create a simple code that will delete sheets automatically by looping through the sheet names: Week 1, Week 2, etc.

到目前为止,这是我的代码:

Here's my code so far:

function myFunction() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet()
  var i = "Week "
  var j = 32
  var mysheet = sheet.getSheetByName(i&j)
  sheet.deleteSheet(mysheet)
}


推荐答案

在您的代码中,您编写了 i& j 而不是在Google Apps脚本中合并的语法。相反,您可以简单地使用 i + j
对于循环,您需要使用任何循环,例如for,while,do-while。

In your code you have written i&j that's not the syntax to concat in Google apps script. Instead you can simply use i + j. For looping you'll need to use any loop, like for, while, do-while.

结合这些建议,这是我的最终建议。

Combining these suggestions, here is my final suggestion.

尝试类似这样的事情[在这里我要使用'try',只是不要复制n-粘贴和使用。尝试理解和编写自己的代码,根据您的特定需求进行策划。也许那样,我们将会成长/了解更多]

Try something like this [By using 'try' here I mean, simply don't copy-n-paste and use. Try to understand and write your own code, curated for your specific need. Maybe that way, we'll grow/learn more]

function myFunction() {
  var START_WEEK = 1; //Put your starting week count here
  var END_WEEK = 2; //Put your ending week count here
  var spreadSheet = SpreadsheetApp.getActiveSpreadsheet()
  var pre_name = "Week"
  var i;
  for (i = START_WEEK; i <= END_WEEK; i++) {
    try {
      spreadSheet.deleteSheet(spreadSheet.getSheetByName(pre_name + " " + i))
    } catch (exp) {
      //Maybe sheet with that name is not found
      Logger.log(exp.toString());
    }
  }
}

此代码循环遍历所有工作表其名称以 Week 开头,然后是周数,直到结束周数,如果找到,则将其删除。

This code loop through all sheet whose name start with "Week " and then followed by week count, up till the end week count and if that's found it's deleting them.

请确保您正确放入 START_WEEK END_WEEK

让我知道这是否对您不起作用或您有任何疑问。

Let me know if this doesn't work for you or if you have any query.

谢谢

这篇关于在Google Apps脚本中串联字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆