Javascript 在 Google Apps 脚本中连接字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49672953/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Concatenating strings in Google Apps Script
提问by wesh
How do I get both values represented by iand jinto the getSheetByNamefunction?
如何获得由i和表示的两个值j到getSheetByName函数中?
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.
免责声明:我是编码新手,可能问错了问题。我的目标是创建一个简单的代码,通过循环工作表名称自动删除工作表:第 1 周、第 2 周等。
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)
}
回答by Umair Mohammad
In your code you have written i&jthat'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.
在您编写的代码中i&j,这不是在 Google 应用程序脚本中连接的语法。相反,您可以简单地使用i + j. 对于循环,您需要使用任何循环,例如 for、while、do-while。
Combining these suggestions, here is my final suggestion.
结合这些建议,这是我的最终建议。
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());
}
}
}
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.
此代码循环遍历名称以 开头"Week "然后是周计数的所有工作表,直到结束周计数,如果找到,则将其删除。
Make sure you put in START_WEEKand END_WEEKproperly.
确保你放入START_WEEK并END_WEEK正确。
Let me know if this doesn't work for you or if you have any query.
如果这对您不起作用或您有任何疑问,请告诉我。
Thanks
谢谢

