是否具有从脚本中排除工作簿中某些工作表的功能 [英] Is there a function to exclude certain sheets from my workbook from the script

查看:65
本文介绍了是否具有从脚本中排除工作簿中某些工作表的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试编写一个代码,以每周一次的基于时间的触发器清除某些数据单元("F2:M95").一切正常,但我注意到它正在清除工作簿中每个工作表上特定单元格中的数据.

I'm trying to write a code to clear certain data cells ("F2:M95") on a time-based trigger once a week. Everything works as I want it to except I noticed it's clearing the data from those particular cells on every sheet in my workbook.

这是我目前拥有的功能:

This is the function I currently have:

function LunchEmailClear() {
  var app = SpreadsheetApp;
  var activeSheet = app.getActive()
  activeSheet.getRange("F2:M95").clear();
}

我可以添加些什么,使其仅适用于某些图纸? (即早晨电子邮件,午餐电子邮件")

What can I add so that it will only apply to certain sheets? (i.e. "Morning Emails, Lunch Emails")

PS.同样抱歉,如果我听起来有些傻眼,我上周刚开始搞乱脚本,几乎不了解我在做什么.感谢您能提供的任何帮助! :)

PS. Also sorry if I sound a bit dumbfounded, I just started messing around with scripts last week and hardly have any clue as to what I'm doing. I appreciate any help I can get! :)

推荐答案

说明:

我们可以利用JavaScript Array.indexOf()方法来实现这一目标.

Explanation:

We can make use of the JavaScript Array.indexOf() method to achieve this.

在下面的脚本中,我们要包含一组工作表并将其定义为var include.然后,我们可以使用运行if语句的for循环将该数组与工作表名称进行比较.这会使用Array.indexOf()根据我们的包含数组检查工作表名称.

In the script below I've an array of sheets we want to include and defined it as var include. We can then compare this array with the sheet names using a for loop running an if statement. This checks the sheet names against our array of inclusions using Array.indexOf().

function emailClear() {
  var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();

  //list sheets to include here
  var include = ['Morning Emails', 'Lunch Emails'];

  for (var i = 0; i < sheets.length; i++) {
    if (include.indexOf(sheets[i].getName()) > -1) {
      sheets[i].getRange("F2:M95").clear();
    }
  }
}


参考文献:

  • Spreadsheet.getSheets()
  • Array.indexOf()
  • Sheet.getName()

  • References:

    • Spreadsheet.getSheets()
    • Array.indexOf()
    • Sheet.getName()
    • 这篇关于是否具有从脚本中排除工作簿中某些工作表的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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