Google Apps Scripting variables and values

When declaring a variable in a Google Apps script, when would I use let vs. const vs var, as in the following?

let sheet = range.getSheet();
const sheet = range.getSheet():
var sheet = range.getSheet();

Different tutorials use different terms. Are they interchangeable? I’d like to choose correctly.

Next, when should one use get.Value() vs get.Values()? I thought one was for a single value and the other for an array of values, but was I thrown an error when reading an email address using get.Value(). The error was: Error sending email to function () { [native code] }. The code worked when I changed it to get.Values().

2

Difference between getValue and getValues

Though the condition on using both is the same, they are expecting different parameters to receive. getValue gets a value of a cell, while the getValues gets 2D array.

Sample Code:

function myFunction() {
  var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var range = ss.getRange("A1:A5").getValues();
  var range2 = ss.getRange("A1:A5").getValue();
  console.log("getValues: " + range)
  console.log("getValue: " + range2)
}

In the sample code above, the line that uses get values will return all values of the range while the line that uses get value will return only the top left value of the range though the range contains lots of values

Sample Output

Data type that getValue() and getValues() handle

getValue() and getValues() also differs from the type that they get, getValue() gets a single type value(string, number) while getValues() handles object types see the output of typeof() method below(which return the type of the parameter given)

Sample Code

function myFunction() {
  var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var range = ss.getRange("A1").getValues();
  var range2 = ss.getRange("A1:A5").getValue();
  console.log("getValues: " + typeof(range))
  console.log("getValue: " + typeof(range2))
}

Sample output

In your case, you might be encountering issue when using getValue due to the structure of the field where the email address is located, most likely, the data you’re accessing (the email address) is part of a range or an item that is being returned as an array (even if it contains only one element)

Difference of let, var and const

In Google Apps Script, which is based on JavaScript, you have three main ways to declare variables: let, const, and var. Each has its own use case and behavior.

  1. let:

    • Scope: Block-scoped (Variable declared using let is only accessible within the block it is defined in).
    • Reassignment: Variables declared with let can be reassigned.
  2. const:

    • Scope: Also Block-scoped (like let).
    • Reassignment: Variables declared with const cannot be reassigned.
  3. var:

    • Scope: Function-scoped (i.e., the variable is accessible throughout the function in which it is defined).
    • Reassignment: Variables declared with var can be reassigned.

When you require a variable that can be changed later, use let. For values that shouldn’t be changed, use const. In current JavaScript, avoid using var because of its scope-related peculiarities.

You can also check this post for more information: var, let and const as explained by Stackoverflow community

Reference:
Var, Let, and Const – What’s the Difference?
getValue()

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

Google Apps Scripting variables and values

When declaring a variable in a Google Apps script, when would I use let vs. const vs var, as in the following?

let sheet = range.getSheet();
const sheet = range.getSheet():
var sheet = range.getSheet();

Different tutorials use different terms. Are they interchangeable? I’d like to choose correctly.

Next, when should one use get.Value() vs get.Values()? I thought one was for a single value and the other for an array of values, but was I thrown an error when reading an email address using get.Value(). The error was: Error sending email to function () { [native code] }. The code worked when I changed it to get.Values().

2

Difference between getValue and getValues

Though the condition on using both is the same, they are expecting different parameters to receive. getValue gets a value of a cell, while the getValues gets 2D array.

Sample Code:

function myFunction() {
  var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var range = ss.getRange("A1:A5").getValues();
  var range2 = ss.getRange("A1:A5").getValue();
  console.log("getValues: " + range)
  console.log("getValue: " + range2)
}

In the sample code above, the line that uses get values will return all values of the range while the line that uses get value will return only the top left value of the range though the range contains lots of values

Sample Output

Data type that getValue() and getValues() handle

getValue() and getValues() also differs from the type that they get, getValue() gets a single type value(string, number) while getValues() handles object types see the output of typeof() method below(which return the type of the parameter given)

Sample Code

function myFunction() {
  var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var range = ss.getRange("A1").getValues();
  var range2 = ss.getRange("A1:A5").getValue();
  console.log("getValues: " + typeof(range))
  console.log("getValue: " + typeof(range2))
}

Sample output

In your case, you might be encountering issue when using getValue due to the structure of the field where the email address is located, most likely, the data you’re accessing (the email address) is part of a range or an item that is being returned as an array (even if it contains only one element)

Difference of let, var and const

In Google Apps Script, which is based on JavaScript, you have three main ways to declare variables: let, const, and var. Each has its own use case and behavior.

  1. let:

    • Scope: Block-scoped (Variable declared using let is only accessible within the block it is defined in).
    • Reassignment: Variables declared with let can be reassigned.
  2. const:

    • Scope: Also Block-scoped (like let).
    • Reassignment: Variables declared with const cannot be reassigned.
  3. var:

    • Scope: Function-scoped (i.e., the variable is accessible throughout the function in which it is defined).
    • Reassignment: Variables declared with var can be reassigned.

When you require a variable that can be changed later, use let. For values that shouldn’t be changed, use const. In current JavaScript, avoid using var because of its scope-related peculiarities.

You can also check this post for more information: var, let and const as explained by Stackoverflow community

Reference:
Var, Let, and Const – What’s the Difference?
getValue()

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật