脚本不清除单元格

时间:2015-04-02 12:51:25

标签: google-apps-script google-docs

我很抱歉地问我这么多读书。脚本完成但不清除选定的单元格。我做错了什么?请帮帮忙?

   // Display a dialog box with a message and "Yes" and "No" buttons.
    var ui = SpreadsheetApp.getUi( );
    var response = ui.alert('Are you sure you want to continue?', ui.ButtonSet.YES_NO);

   // Process the user's response.
   if (response == ui.Button.YES) {
   Logger.log('The user clicked "Yes."');
   } else {
   Logger.log('The user clicked "No" or the dialog\'s close button.');
   }  
   function clearRange() {
  //replace 'Sheet1' with your actual sheet name
  var sheet = SpreadsheetApp.getActive().getSheetByName('LOAD ASSIGNMENT');
  sheet.getRange('A4:L120').clearContent();
   }

2 个答案:

答案 0 :(得分:1)

Warning: unused function clearRange.打电话给它:) 定义函数后,您将丢失:
clearRange();

答案 1 :(得分:1)

正如其他人一样,你已经完成了功能clearRange,但是你没有在哪里激活/调用它。我以为我已经包含了更改过的代码,因此您可以通过调用函数来查看其含义。

    function myFunction() {

    function clearRange() {
  //replace 'Sheet1' with your actual sheet name
  var sheet = SpreadsheetApp.getActive().getSheetByName('LOAD ASSIGNMENT');
  sheet.getRange('A4:L120').clearContent();
   }


   // Display a dialog box with a message and "Yes" and "No" buttons.
    var ui = SpreadsheetApp.getUi( );
    var response = ui.alert('Are you sure you want to continue?', ui.ButtonSet.YES_NO);

   // Process the user's response.
   if (response == ui.Button.YES) {

     // Just to clarify, you have created the clearRange function. I've moved it above. But no where do you say run it.
     // Below is the function being CALLED if the response to the prompt is YES


  clearRange();  


   Logger.log('The user clicked "Yes."');
   } else {
   Logger.log('The user clicked "No" or the dialog\'s close button.');
   }  


}