IF功能 - Google脚本 [英] IF Function - Google Scripts

查看:76
本文介绍了IF功能 - Google脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我努力让脚本运行一个IF函数。基本上我想运行一个基于特定单元格内容的脚本。



我想要一个基于这个函数运行的IF函数,并写下如下代码:

  function sendemail(){


var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = SpreadsheetApp.getActiveSheet();

var targetSheet = ss.getSheetByName(Response);
var vCodes = ss.getSheetByName(Codes)

var vResults = targetSheet.getRange(E2)。getValues();
var emailAddresses = targetSheet.getRange(B2)。getValues()

var dataRange = vCodes.getRange(1,1,vResults,1).getValues();
var subject =这是您的Wi-Fi代码!;
var vLength = vCodes.getRange(C2)。getValues();

if(vLength ==24 hours){
MailApp.sendEmail(emailAddresses,subject,dataRange);
targetSheet.deleteRows(2);
vCodes.deleteRows(1,vResults);






如果C2中的值是24小时I '喜欢它发送电子邮件。当我运行脚本的时候,没有错误,但它不会发送任何电子邮件,因为IF函数显然运行不正常。



如果我编辑代码说:

  if(vLength ==)

然后发送电子邮件。它似乎并不认可24小时作为有效的数据来查找。




  • 任何人都可以看到我做错了什么吗?


解决方案

您从单元格获得的值不是您的想法,因为您正在使用 getValues()与一个's',你可能知道这个方法总是返回一个数组数组,即使单个单元格被定义为范围。
$ b

您有两种选择:
$ b $ ol

  • 使用 getValue()获取单元格的字符串内容
  • 使用 getValues()[0] [0] 来获取第一个只有)这个数组的元素。

    我会建议第一个解决方案,因为我认为使用合适的方法通常是个好主意。对于单元格, getValue(),对于多个单元格, getValues() ...
    我didn没有进一步检查,但我很确定它会适用于此更改(适用于 vRes ults emailAddresses vLength )。

    同样要小心确保 vResults 是一个数字,因为您使用它来定义范围......您可以使用 Number(vResults)作为安全措施。


    I'm struggling to get my Script to run an IF function. Basically I want to run a script based on specific cell contents.

    I would like an IF function to run based on this and have written the following code:

    function sendemail () {
    
    
     var ss = SpreadsheetApp.getActiveSpreadsheet(); 
     var s = SpreadsheetApp.getActiveSheet(); 
    
     var targetSheet = ss.getSheetByName("Response");
     var vCodes = ss.getSheetByName("Codes") 
    
     var vResults = targetSheet.getRange("E2").getValues(); 
     var emailAddresses = targetSheet.getRange("B2").getValues() 
    
     var dataRange = vCodes.getRange(1, 1, vResults, 1).getValues();
     var subject = "Here are your Wi-Fi Codes!";
     var vLength = vCodes.getRange("C2").getValues();  
    
     if (vLength == "24 hours"){
     MailApp.sendEmail(emailAddresses, subject, dataRange);
     targetSheet.deleteRows(2);
     vCodes.deleteRows(1,vResults);
        }
    }
    

    If the value in C2 is "24 hours" I'd like it to send an e-mail. At the moment when I run the script there are no errors but it doesn't send any e-mail as the IF function obviously isn't running correctly.

    If I edit the code to say:

     if (vLength == "")
    

    then the e-mail sends. It doesn't seem to recognise "24 hours" as valid data to look up.

    • Can anyone see what I'm doing wrong?

    解决方案

    The value you get from the cell is not what you think because you are using getValues() with an 's' and you probably know that this method always returns an array of arrays, even when a single cell is defined as range.

    You have 2 options :

    1. use getValue() to get the string content of the cell
    2. use getValues()[0][0] to get the first (and only) element of this array.

    I would suggest the first solution as I think it's generally a good idea to use appropriate methods... getValue() for single cell and getValues() for multiple cells... I didn't check further but I'm pretty sure it will work with this change (applies to vResults , emailAddresses and vLength) .

    It would also be careful to ensure that vResults is a number since you use it to define a range... you could use Number(vResults) as a safety measure.

    这篇关于IF功能 - Google脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆