javascript - Equivalent of "GoTo" in Google Apps Script (equivalent VBA-GAS ) - Stack Overflow

When writing my VBA macros I often used "GoTo" so as to jump to a previous part of the macro

When writing my VBA macros I often used "GoTo" so as to jump to a previous part of the macro without leaving the Sub. Now that I’m converting all my macros to Google Apps Script I’m trying to find the equivalent for “GoTo”.

Sub MySub()
Dim sheetname1 As String
Dim sheetname2 As String
On Error GoTo Err
       sheetname1 = ActiveSheet.Name
           Sheets.Add After:=Sheets(Sheets.Count)
           ActiveSheet.Name = "passwords"
       sheetname2 = ActiveSheet.Name
GoTo aftererr
Err:
MsgBox Error(Err)
Exit Sub
aftererr:

This is just one instance of my use of GoTo. However I need it for my new scripts in many other ways; not just for redirecting errors. For example:

 function MyFunction() {
 var ss = SpreadsheetApp.getActiveSpreadsheet();
 var sh = ss.getActiveSheet();
 if(criteraA == criteraB){
 sh.offset(1,0).activate();
 var i=i + 1;
 //?? GoTo ??
 }else{
 var i=0;
 sh.getRange(row, column)(1,sr.offset(0,1).getColumn()).activate();
 }

When writing my VBA macros I often used "GoTo" so as to jump to a previous part of the macro without leaving the Sub. Now that I’m converting all my macros to Google Apps Script I’m trying to find the equivalent for “GoTo”.

Sub MySub()
Dim sheetname1 As String
Dim sheetname2 As String
On Error GoTo Err
       sheetname1 = ActiveSheet.Name
           Sheets.Add After:=Sheets(Sheets.Count)
           ActiveSheet.Name = "passwords"
       sheetname2 = ActiveSheet.Name
GoTo aftererr
Err:
MsgBox Error(Err)
Exit Sub
aftererr:

This is just one instance of my use of GoTo. However I need it for my new scripts in many other ways; not just for redirecting errors. For example:

 function MyFunction() {
 var ss = SpreadsheetApp.getActiveSpreadsheet();
 var sh = ss.getActiveSheet();
 if(criteraA == criteraB){
 sh.offset(1,0).activate();
 var i=i + 1;
 //?? GoTo ??
 }else{
 var i=0;
 sh.getRange(row, column)(1,sr.offset(0,1).getColumn()).activate();
 }
Share Improve this question edited Jul 9, 2018 at 19:34 CommunityBot 11 silver badge asked Jun 17, 2013 at 18:05 Frank MontemoranoFrank Montemorano 3214 gold badges6 silver badges12 bronze badges 1
  • More on that: stackoverflow./questions/17203074/… – Frank Montemorano Commented Jul 19, 2013 at 21:15
Add a ment  | 

1 Answer 1

Reset to default 4

You don't need GoTo, most people would argue that it is terrible programming practice to use it even when it is present. Using other control structures will do the job.

if() {
} else if() {
} else {
}

for(;;) {
   continue;
   break;
}

while() {
}

do {
} while();

switch() {
case:
default:
}

// for errors
throw "Error string"

try {
} catch(error) {
}

You'll have to shuffle your logic around a bit, but it will result is better more maintainable code.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744210839a4563324.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信