使谷歌脚本更紧凑(清除内容的脚本)

时间:2016-04-13 21:36:13

标签: google-apps-script google-sheets

所以我有两个在行和列以及单元格位置相同的计划。有些单元格将始终保持不变,但有些单元格将会发生变化。所以我有一个自定义菜单脚本,它将调用两个相同的工作表之一并清除预定义的单元格。在脚本方面,基本上我只是复制/粘贴相同的脚本两次并更改了活动表名称。但是,我觉得它太多了。有没有办法让我只有一个脚本,根据我选择的菜单项,它会擦除​​相应的时间表?

这是自定义菜单脚本

  function resetsheetbutton() {
  var ui = SpreadsheetApp.getUi();
  ui.createMenu('Advanced Menu')
     .addSubMenu(ui.createMenu('Reset Schedule')
          .addItem('Schedule 1', 'resetschedule1')
          .addSeparator()
          .addItem('Schedule 2', 'resetschedule2'))
        .addToUi();
 } 

这是清除Schedule 1

的脚本
function resetschedule1(){
   var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
 SpreadsheetApp.setActiveSheet(spreadsheet.getSheetByName('Notification'));
   var sheet = spreadsheet.getActiveSheet();
   var spreadsheet1 = SpreadsheetApp.getActiveSpreadsheet();
 SpreadsheetApp.setActiveSheet(spreadsheet1.getSheetByName('Schedule 1'));
   var sheet1 = spreadsheet1.getActiveSheet();
   var startRow = 2;
   var lastRow = 80;
   var range = sheet.getRange(2,11,lastRow-startRow+1,1 );
   var numRows = range.getNumRows();
   var reset = range.getValues();


 for (var i = 0; i < numRows; i++) {
 var row = reset[i];
 sheet1.getRange(row).clearContent();
 }
   sheet1.getRange('C3').setValue('Insert new Date here');
   sheet1.getRange('D3').setValue('Insert this week's task');
   sheet1.getRange('D29').setValue('Insert this week's task');
   sheet1.getRange('D55').setValue('Insert this week's task');
   sheet1.getRange('D81').setValue('Insert this week's task');
   sheet1.getRange('D107').setValue('Insert this week's task');
   sheet1.getRange('C6').setValue('=C5');
   sheet1.getRange('C25').setValue('=C5');
   sheet1.getRange('C32').setValue('=C31');
   sheet1.getRange('C51').setValue('=C31');  
   sheet1.getRange('C58').setValue('=C57');
   sheet1.getRange('C77').setValue('=C57');  
   sheet1.getRange('C84').setValue('=C83');
   sheet1.getRange('C103').setValue('=C83');  
   sheet1.getRange('C110').setValue('=C109');
   sheet1.getRange('C129').setValue('=C109'); 
 }

这与计划2的确切脚本相同,只是它指向计划2。 我特意为工作表命名,而不是让它擦除活动表格,这样一来,清除时间表的人就不会意外地清除错误的时间表。

function resetschedule2(){
   var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
 SpreadsheetApp.setActiveSheet(spreadsheet.getSheetByName('Notification'));
   var sheet = spreadsheet.getActiveSheet();
   var spreadsheet1 = SpreadsheetApp.getActiveSpreadsheet();
 SpreadsheetApp.setActiveSheet(spreadsheet1.getSheetByName('Schedule 2'));
   var sheet1 = spreadsheet1.getActiveSheet();
   var startRow = 2;
   var lastRow = 80;
   var range = sheet.getRange(2,11,lastRow-startRow+1,1 );
   var numRows = range.getNumRows();
   var reset = range.getValues();


 for (var i = 0; i < numRows; i++) {
 var row = reset[i];
 sheet1.getRange(row).clearContent();
 }
   sheet1.getRange('C3').setValue('Insert new Date here');
   sheet1.getRange('D3').setValue('Insert this week's task');
   sheet1.getRange('D29').setValue('Insert this week's task');
   sheet1.getRange('D55').setValue('Insert this week's task');
   sheet1.getRange('D81').setValue('Insert this week's task');
   sheet1.getRange('D107').setValue('Insert this week's task');
   sheet1.getRange('C6').setValue('=C5');
   sheet1.getRange('C25').setValue('=C5');
   sheet1.getRange('C32').setValue('=C31');
   sheet1.getRange('C51').setValue('=C31');  
   sheet1.getRange('C58').setValue('=C57');
   sheet1.getRange('C77').setValue('=C57');  
   sheet1.getRange('C84').setValue('=C83');
   sheet1.getRange('C103').setValue('=C83');  
   sheet1.getRange('C110').setValue('=C109');
   sheet1.getRange('C129').setValue('=C109'); 
 }

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

由于菜单中的功能不能包含参数,您最初仍需要两个功能,但它们会很短:

function resetschedule1() {
  resetschedule("1");//Pass the string value "1" to the function
};

function resetschedule2() {
  resetschedule("2");//Pass the value "2"
};

您可以创建一个对象来设置两个工作表中相同的值。使用JavaScript对象,您可以匹配&#34;键&#34;到了&#34;价值&#34;。在这种情况下,&#34;键&#34;将是单元格地址,值将是值。

var objCellToValueSameForBoth = {
  "C3":"Insert new Date here",
  "D3":"Insert this week's task"
};

//Set the values that are the same in both sheets
for (var key in objCellToValueSameForBoth) {
  sheet.getRange(key).setValue(objCellToValueSameForBoth[key]);
};

您可以通过多种方式让代码使用不同的值。您可以将值传递给函数:

function resetschedule(argWhichSheet) {

  var objArgToSheetName = {"1":"Schedule 1","2":"Schedule 2"};
  var whichSheetToClear = objArgToSheetName[argWhichSheet];

  SpreadsheetApp.setActiveSheet(spreadsheet1.getSheetByName(whichSheetToClear));
};