Concatenating strings in Google Apps Script

前端 未结 1 1320
春和景丽
春和景丽 2020-12-20 16:46

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

Disclaimer: I am brand new at coding and am

相关标签:
1条回答
  • 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 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.

    Make sure you put in START_WEEK and END_WEEK properly.

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

    Thanks

    0 讨论(0)
提交回复
热议问题