新电子表格; .clearContent();获取用户尝试编辑受保护单元的错误

时间:2014-11-11 14:27:04

标签: google-apps-script google-sheets

在新的Google电子表格中,我有一个大型脚本,可以清除8个不同工作表中许多单元格中的内容。

总计工作表的公式是唯一受保护的部分,但我一直在.clearContent()上得到错误;脚本未触及任何受保护单元格时的方法。

有什么想法?这是旧电子表格中使用的相同脚本(完美工作)。

function masterCloseWeek() { 
 var hideLiqOrder = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('LIQUOR ORDER');
 hideLiqOrder.hideSheet();
 var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
 var costReport = spreadsheet.getSheetByName('COST REPORT').activate();//Moves sheet to position 1
 spreadsheet.moveActiveSheet(1);
 var invoiceLog = spreadsheet.getSheetByName('INVOICE LOG').activate();//Moves sheet to position 2
 spreadsheet.moveActiveSheet(2);
 var dailyInventory = spreadsheet.getSheetByName('DAILY INVENTORY').activate();//Moves sheet to position 3
 spreadsheet.moveActiveSheet(3);
 SpreadsheetApp.flush();

  var spreadsheetId = 'xxxxxxxxxxxx'; //ID
  var file = Drive.Files.get(spreadsheetId);
  var url = file.exportLinks['application/pdf'];
  var token = ScriptApp.getOAuthToken();
  var response = UrlFetchApp.fetch(url, {
    headers: {
      'Authorization': 'Bearer ' +  token
    }
  });
  //var pdf = response.getBlob();
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getName();
  var pdf = response.getBlob().setName(ss.getName() + '.pdf');
  var CopyDate = Utilities.formatDate(new Date(), "GMT-3", "MM/dd/yyyy   HH:mm"); // Funtion Date + Format

DocsList.createFile(pdf);
MailApp.sendEmail({
to:"myEmail@test.com",
subject: "Blank (v2.0) Weekly File Attached" + "_" + "Sent on " + CopyDate,
body:"(This is an automated email.).....Attached is a copy of the weekly file sent on" + "      "+CopyDate,
attachments: [pdf]
  })

  var hideLiqOrder = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('LIQUOR ORDER');
  hideLiqOrder.showSheet();

  var liquorInventory = SpreadsheetApp.getActive().getSheetByName('LIQUOR INVENTORY');
  liquorInventory.getRange('J6:J588').copyTo(liquorInventory.getRange('M6:M588'),{contentsOnly:true});
  var foodInventory = SpreadsheetApp.getActive().getSheetByName('FOOD INVENTORY');
  foodInventory.getRange('G5:G622').copyTo(foodInventory.getRange('I5:I622'),{contentsOnly:true});

//Finished closing week

//Starting a new week  
  var activeSheet = SpreadsheetApp.getActiveSpreadsheet(); 
  var costReport = activeSheet.getSheetByName('COST REPORT'); 
  var FraminghamSnapShot = activeSheet.getSheetByName('FraminghamSnapShot');
  costReport.getRange('E12:E16').copyTo(FraminghamSnapShot.getRange('B23:B27'),{contentsOnly:true});

这是第一个错误,当它尝试清除invoiceLog.getRange('A3:L52').clearContent();

var invoiceLog = SpreadsheetApp.getActive().getSheetByName('INVOICE LOG');
    invoiceLog.getRange('A3:L52').clearContent();
  var dailyInventory = SpreadsheetApp.getActive().getSheetByName('DAILY INVENTORY');
    dailyInventory.getRange('C5:C9').clearContent();
    dailyInventory.getRange('D6:I9').clearContent();
    dailyInventory.getRange('C13:C17').clearContent();
    dailyInventory.getRange('D14:I17').clearContent();
    dailyInventory.getRange('C21:C25').clearContent();
    dailyInventory.getRange('D22:I25').clearContent();
    dailyInventory.getRange('C29:C33').clearContent();
    dailyInventory.getRange('D30:I33').clearContent();
    dailyInventory.getRange('C37:C41').clearContent();
    dailyInventory.getRange('D38:I41').clearContent();
    dailyInventory.getRange('C45:C49').clearContent();
    dailyInventory.getRange('D46:I49').clearContent();
    dailyInventory.getRange('C55:C59').clearContent();
    dailyInventory.getRange('D56:I59').clearContent();
    dailyInventory.getRange('C63:C67').clearContent();
    dailyInventory.getRange('D64:I67').clearContent();
    dailyInventory.getRange('C71:C75').clearContent();
    dailyInventory.getRange('D72:I75').clearContent();
    dailyInventory.getRange('C79:C83').clearContent();
    dailyInventory.getRange('D80:I83').clearContent();
    dailyInventory.getRange('C87:C91').clearContent();
    dailyInventory.getRange('D88:I91').clearContent();
    dailyInventory.getRange('C95:C99').clearContent();
    dailyInventory.getRange('D96:I99').clearContent();
    dailyInventory.getRange('C105:C109').clearContent();
    dailyInventory.getRange('D106:I109').clearContent();
    dailyInventory.getRange('C113:C117').clearContent();
    dailyInventory.getRange('D114:I117').clearContent();
    dailyInventory.getRange('C121:C125').clearContent();
    dailyInventory.getRange('D122:I125').clearContent();
    dailyInventory.getRange('C129:C133').clearContent();
    dailyInventory.getRange('D130:I133').clearContent();
    dailyInventory.getRange('C137:C141').clearContent();
    dailyInventory.getRange('D138:I141').clearContent();
    dailyInventory.getRange('C145:C149').clearContent();
    dailyInventory.getRange('D146:I149').clearContent();
  var foodInventory = SpreadsheetApp.getActive().getSheetByName('FOOD INVENTORY');
     foodInventory.getRange('D5:F622').clearContent();
  var liquorInventory = SpreadsheetApp.getActive().getSheetByName('LIQUOR INVENTORY');
    liquorInventory.getRange('E6:I371').clearContent(); //Vodka thru Wines
    liquorInventory.getRange('G375:I433').clearContent(); //Draft Beer
    liquorInventory.getRange('G437:I535').clearContent(); //Bottle Beer
    liquorInventory.getRange('E539:I588').clearContent(); //Non Alcoholic
  var dailySalesSheet = SpreadsheetApp.getActive().getSheetByName('DAILY SALES SHEET');
    dailySalesSheet.getRange('B4:H11').clearContent(); //Sales Categories
  var costReport = SpreadsheetApp.getActive().getSheetByName('COST REPORT');
    costReport.getRange('F12:F16').clearContent(); //Theoreticals
    costReport.getRange('D20:D20').clearContent(); //Week Ending Date
  var safeAudit = SpreadsheetApp.getActive().getSheetByName('SAFE AUDIT');
    safeAudit.getRange('C3:P11').clearContent();
    safeAudit.getRange('C14:P18').clearContent();
    safeAudit.getRange('C22:P22').clearContent();
  var destination = SpreadsheetApp.getActiveSpreadsheet();
  Browser.msgBox('Closed Successfully','Your week has been successfully closed\\n and emailed.', Browser.Buttons.OK);
    var name = Browser.inputBox('Enter Week Ending Date ONLY', 'Example:     11-5-14 \\n\\n', Browser.Buttons.OK);   
   destination.rename("BlankWE" + name);
   costReport.getRange('D20').setValue(name);
}

有更好的更有效的清除细胞的方法吗?这有助于解决问题吗? 肖恩。

1 个答案:

答案 0 :(得分:1)

好吧,我在invoiceLog.getRange('A3:L52').clearContent();找不到任何错误,这对我来说非常合适。