将VBA转换为Java Google脚本

时间:2018-11-18 13:34:42

标签: javascript vba google-apps-script

具有以下VBA,我需要将其插入Google表格中,

有人可以帮助转换吗?

Sub Activate_Sheet()
    Sheets(Sheets("Main").Range("A1").Value).Activate
End Sub

谢谢

1 个答案:

答案 0 :(得分:0)

此脚本获取sheet()[0]中单元格A1中的值,然后移至该工作表编号。

function so_53361440() {
  // set up spreadsheet
    var ss = SpreadsheetApp.getActiveSpreadsheet();

    // set sheet 0 as the active sheet
  var sheet = ss.getSheets()[0];

    // get the value in cell A1
  var cell = sheet.getRange("A1").getValue();
  // Logger.log("value of cell = "+cell);// DEBUG

    // Convert number to string
  var sheetname = ""+ cell;
  // Logger.log("sheetname = "+sheetname);// DEBUG

    // set sheet by name and move to new sheet
    var mysheet = ss.getSheetByName(sheetname);
    ss.setActiveSheet(mysheet);
}

主题变化
对于300张纸,返回sheet()[0]会令人沮丧。因此,此较小的变化旨在创建一个自定义菜单,该菜单将在输入框中请求工作表编号。其余代码相同

function so_53361440_01() {
    // setup ui
  var ui = SpreadsheetApp.getUi(); 

  var result = ui.prompt(
      'What sheet do you want?',
      'Please enter a number:',
      ui.ButtonSet.OK_CANCEL);

  // Process the user's response.
  var button = result.getSelectedButton();
  var text = result.getResponseText();
  if (button == ui.Button.OK) {
    // User clicked "OK".
    //ui.alert('Sheet is ' + text + '.');
  } else if (button == ui.Button.CANCEL) {
    // User clicked "Cancel".
    ui.alert('Operation Cancelled.');
  } else if (button == ui.Button.CLOSE) {
    // User clicked X in the title bar.
    ui.alert('Input Box closed - no action taken.');
  }

    // set up spreadsheet
  var ss = SpreadsheetApp.getActiveSpreadsheet();

    // assign the UI value to a variable
  var cell = text;
  // Logger.log("value of cell = "+cell);//DEBUG

    // convert the variable to a string
  var sheetname = ""+ cell;
  // Logger.log("sheetname = "+sheetname);// DEBUG

    // set the sheetname to the variable and goto that sheet
    var mysheet = ss.getSheetByName(sheetname);
    ss.setActiveSheet(mysheet);

}

function onOpen() {
 SpreadsheetApp.getUi() 
      .createMenu('Change Sheet')
      .addItem('Pick a sheet', 'so_53361440_01')
      .addToUi();
}