Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google apps script 基于其他单元格值应用ClearContents函数_Google Apps Script_Google Sheets_Google Sheets Api - Fatal编程技术网

Google apps script 基于其他单元格值应用ClearContents函数

Google apps script 基于其他单元格值应用ClearContents函数,google-apps-script,google-sheets,google-sheets-api,Google Apps Script,Google Sheets,Google Sheets Api,我有一个谷歌工作表,每天都有很多人使用,每天工作表都必须“重启”和“备份”。到目前为止,我使用ClearRange函数和MakeCopy函数将整个文件保存在我的另一个GoogleSheet文件夹中。这些功能正在基于时间的触发器上工作,我发现这与我无关,我想通过OnEdit功能激活它们,该功能将基于单元格值,并创建一种同样适用于Android用户的“菜单” 这是我到目前为止根据时间触发器所做的: function clearRange() { var sheet = SpreadsheetAp

我有一个谷歌工作表,每天都有很多人使用,每天工作表都必须“重启”和“备份”。到目前为止,我使用ClearRange函数和MakeCopy函数将整个文件保存在我的另一个GoogleSheet文件夹中。这些功能正在基于时间的触发器上工作,我发现这与我无关,我想通过OnEdit功能激活它们,该功能将基于单元格值,并创建一种同样适用于Android用户的“菜单”

这是我到目前为止根据时间触发器所做的:

function clearRange() {
  var sheet = SpreadsheetApp.getActive().getSheetByName('work_sheet');
  sheet.getRange('A5:L200').clearContent();
  sheet.getRange('O5:P200').clearContent();
}



function makeCopy() {
// generates the timestamp and stores in variable formattedDate as year-month-date hour-minute-second
var formattedDate = Utilities.formatDate(new Date(), "GMT", "yyyy-MM-dd' 'HH:mm:ss");

// gets the name of the original file and appends the word "copy" followed by the timestamp stored in formattedDate
var name = SpreadsheetApp.getActiveSpreadsheet().getName() + " Copy " + formattedDate;

// gets the destination folder by their ID. REPLACE xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx with your folder's ID that you can get by opening the folder in Google Drive and checking the URL in the browser's address bar
var destination = DriveApp.getFolderById("xxxxxxxxx(my google sheet folder URL)");

// gets the current Google Sheet file
var file = DriveApp.getFileById(SpreadsheetApp.getActiveSpreadsheet().getId())

// makes copy of "file" with "name" at the "destination"
file.makeCopy(name, destination);
}
我发现了一些我试图应用于我的函数但没有成功的代码:

var FunctionsCell = "B2"  // global
function onEdit(e) {    
  var editCell = e.range.getA1Notation()

switch (editCell) {
case FunctionsCell: {               
      var functionType = SpreadsheetApp.getActiveSheet().getRange(FunctionsCell).getValue()

      switch(functionType) {              
        case "Do Task 1": {
          // do something
          break
        }
        case "Do Task 2": {
          // do something
          break
        }
      }
  }
  }
}
(在这一次中,它给出了关于“var editCell=e.range.getA1Notation()的错误信息。)

只有当我没有给出一个特定的范围时,它才起作用

function onEdit3(e) {
  var ss = SpreadsheetApp.getActive()
  var sheet = SpreadsheetApp.getActiveSheet()
  var cell = sheet.getRange('O1')
  var cellContent = cell.getValue()

  if(cellContent === 100) {
    sheet.getRange('A5:L10').clearContents()
  }
}

我希望它能成为android用户点击一个选项或编写一个特定单词的菜单,以使这两个功能正常工作。谢谢大家,我需要更多的细节来完成这项工作。但这种方法应该有效。请记住,您不能从脚本编辑器调用这些功能或从菜单启动它们。您可以必须实际使用onEdit()触发器运行它们才能显示事件对象。这种调试可能很困难,通常会提高您的故障排除技能,或者可能会让您放弃

function onEdit(e) {    
  var sh=e.range.getSheet();
  var name=sh.getName();
  if(name !='Sheet1') return;//limits the functionality to one sheet
  switch (e.range.getA1Notation()) {
    case 'B2': {               
      var functionType = e.range.getSheet().getRange("B2").getValue()
      switch(functionType) {              
        case "task1": {
          // do something
          break
        }
        case "task2": {
          // do something
          break;
        }
      }
    }
  }
}

您的代码给我一个错误类型错误:无法从undefined中读取属性“range”。(第2行,文件“code”)dismiss是,脚本将在每次编辑文件时运行,因此每当“task1”是这个函数将要运行的B2的值。你能告诉我你还需要什么信息吗?你有两个函数要运行,分别调用一个函数和另一个函数