简体   繁体   English

Google Apps 脚本不起作用?

[英]Google Apps Script not functioning?

I'm no wizard, but I've been playing around trying to learn a bit with google apps script and I'm just confused as to why it isn't working.我不是巫师,但我一直在尝试用谷歌应用脚本学习一点,我只是对它为什么不起作用感到困惑。 I've looked through the documentation and have referenced other works using if + else scripts and I can't find the error.我查看了文档并使用 if + else 脚本引用了其他作品,但我找不到错误。 Context: E7 is a checkbox, F7 is a fill-in-the-blank.上下文:E7 是一个复选框,F7 是一个填空。

function testO() {
  var mySS = SpreadsheetApp.getActiveSpreadsheet();
  var getRange = mySS.getRange("E7")
  
  
  if(getRange = true){
    mySS.getRange("E7").setValue("FALSE")
  }
  else{
    mySS.getRange("F7").clearContent()
  }
}

It looks that you need to spend some time learning the pretty basics of JavaScript.看来您需要花一些时间学习 JavaScript 的基本知识。 Please start by reading https://developers.google.com/apps-script/guides/sheets as it gives helpful pointers like,请从阅读https://developers.google.com/apps-script/guides/sheets开始,因为它提供了有用的指示,例如,

  • Google Apps Script programming language is JavaScript Google Apps 脚本编程语言是 JavaScript
  • How to read and write to a Google Sheets spreadsheet.如何读取和写入 Google 表格电子表格。

Regarding the specifics of your code,关于您的代码的细节,

  1. in the mySs.getRange methods, instead of A1 use Sheet1!A1 style referencesmySs.getRange方法中,使用Sheet1!A1样式引用而不是A1
  2. in the comparison expressions for equality in your if statement, instead of the assign operator = use abstract equality == or strict equality === operators, but in this very specific case it might be better to use Range.isChecked() .在 if 语句中相等的比较表达式中,使用抽象相等==或严格相等===运算符而不是赋值运算符= ,但在这种非常特殊的情况下,使用Range.isChecked()可能会更好。
  3. There are specific methods for working with checkboxes: Range.isChecked() , Range.uncheck() , Range.check() ;有使用复选框的特定方法: Range.isChecked()Range.uncheck()Range.check()

Below is a proposal to fix your script:以下是修复脚本的建议:

function testO() {
  var mySS = SpreadsheetApp.getActiveSpreadsheet();
  var getRange = mySS.getRange("Sheet1!E7");
  
  
  if(getRange.isChecked()){
    getRange.uncheck();
  }
  else{
    mySS.getRange("Sheet1!F7").clearContent();
  }
}

Notes:笔记:

  • Google Apps Script use the same method name for some methods that return the same type of object, ie Spreadsheet.getRange , Sheet.getRange , but they might require different parameters, Google Apps 脚本对返回相同类型 object 的某些方法使用相同的方法名称,即Spreadsheet.getRangeSheet.getRange ,但它们可能需要不同的参数,

  • Spreadsheet.getRange might use A1 or Sheet1!A1 style references. Spreadsheet.getRange可能使用A1Sheet1!A1样式引用。 When using A1 it will get the cell A1 from the first sheet.使用A1时,它将从第一张表中获取单元格A1

  • Sheet.getRange might use A1 style but it will return the cell A1 from the corresponding sheet. Sheet.getRange可能使用A1样式,但它会从相应的工作表返回单元格A1

A simple example with some explanation一个简单的例子,有一些解释

function testO() {
  const ss = SpreadsheetApp.getActive();//I like havign access to the spreadsheet as a single variable.  While it may not be important now it might be in the future.
  const sh = ss.getSheetByName("Sheet0");//For clerity sake it is better to identify the sheet you are going to.  In your code you depended upon the fact that the spreadsheet has a default active sheet which is ss.getSheets()[0]
  const rg = sh.getRange("E7");//getRange returns a range no a value
  const v = rg.getValue();//getValue returns a single value
  
  if(v == true){ //= is an assignment == is a comparison 
    sh.getRange("E7").setValue("FALSE");//This is usually the value reserved for an unchecked checkbox
  }
  else{
    rg.clearContent()
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM