修正轉換程式碼中的錯誤

巨集轉換程式外掛程式可自動執行大部分的轉換程序,但您可能需要調整某些 API 和其他項目,才能完成程式碼。

本指南可協助您瞭解已新增至專案的 Apps Script 檔案 (GS 檔案)、解讀不同的錯誤類型,以及瞭解如何修正錯誤。

瞭解已新增至專案的 Apps Script 檔案

系統會在 Apps Script 專案中加入更多 GS 檔案,以提供下列協助:

  • 定義 Apps Script 中沒有的 VBA 常數和值。
  • 實作未轉換的 API。
  • 解析變化版本。

系統會將下列 GS 檔案新增至 Apps Script 專案:

  • Library.gs
  • Unimplemented_constructs.gs
  • Variant_resolutions.gs

Library.gs

一般來說,您不需要修改 library.gs 檔案中的所有內容。

library.gs 檔案定義了 Apps Script 中沒有的 VBA 程式碼中使用的函式和常數。如此一來,新的 Apps Script 程式碼就能與您的 VBA 程式碼更相似。此外,您不需要每次使用 library.gs 檔案中的函式或常數時,都重複定義。

Unimplemented_constructs.gs

unimplemented_constructs.gs 檔案會解析巨集轉換工具無法轉換的建構或 API。您可能需要修改這個檔案,程式碼才能正常運作。

範例:Window.Activate()

以下為不支援的 API 示例:Window.Activate()。巨集轉換工具會使用名稱相似的新 Apps Script 函式,在 unimplemented_constructs.gs 檔案中定義該函式。由於系統不支援 VBA 函式,因此新的 Apps Script 函式會擲回例外狀況。

新函式會新增至轉換後的 Apps Script 程式碼,任何 VBA 程式碼中都使用原始 API 的地方。

如果您發現可重新建立原始 API 行為的解決方法,則只需更新 unimplemented_constructs.gs 檔案中的函式定義。在這裡定義函式後,即會套用至 Apps Script 專案中出現該函式的所有位置。

程式碼範例如下:

原始 VBA 代碼

Window.activate()

轉換後的 Apps Script 程式碼,已新增至內嵌程式碼

_api_window_activate();

unimplemented_constructs.gs 檔案中加入函式定義

/**
 * Could not convert window.activate API. Please add relevant code in the
 * following function to implement it.
 * This API has been used at the following locations in the VBA script.
 *     module1 : line 3
 *
 * We couldn't find an equivalent API in Apps Script for this VBA API. Please
 * reconsider if this function call is critical, otherwise consider implementing
 * it in a different way.
 */
function _api_window_activate(CallingObject) {
  ThrowException("API window.activate not supported yet.");
}

Variant_resolutions.gs

無法判斷物件類型時,系統會將 variant_resolutions.gs 檔案新增至 Apps Script 專案。造成這種情況的���因很多,例如 API 有多個傳回類型,或是該物件本身宣告為變化版本。

巨集轉換工具會在這個檔案中新增名為 __handle_resolve_<api>() 的函式,取代有問題的 API,並協助判斷物件類型。

在某些情況下,您可能需要更新 __handle_resolve_<api>() 函式,手動宣告物件類型。請參閱不支援的物件類型

範例:name()

VBA 中有許多物件類型都定義 name() API。一般來說,與 Apps Script 相等的指令碼是 getName(),但不是每個物件類型都適用。可能會發生多種替代情況:

  • 物件對應 API 的呼叫方式與 getName() 不同。
  • 物件沒有 Apps Script API,因此無法取得名稱。
  • 沒有對等的 Apps Script 物件。

無法識別物件類型時,巨集轉換工具會在 variant_resolutions.gs 檔案中建立名為 __handle_resolve_name 的新函式。

程式碼範例如下:

原始 VBA 代碼

a = Selection.name

在這個例子中,系統會在目前選取的項目上呼叫 API name()。所選項目可以是工作表物件或形狀物件。如果是 Sheet 物件,翻譯為 getName(),但如果是 Shape 物件,則 Apps Script 中沒有對等項目,

轉換後的 Apps Script 程式碼,已新增至內嵌文字

a = __handle_resolve_name({}, getActiveSelection(), {});

系統會將下列 __handle_resolve_name() 函式新增至 variant_resolution.gs 檔案,以便解決不同物件類型的問題。這個函式會檢查物件類型,如果支援該類型,則會使用 getName()。如果不支援 getName(),則會擲回錯誤。

variant_resolution.gs 檔案中加入函式定義

function __handle_resolve_name(ExecutionContext, CallingObject, params_map) {
  var found_api_variant = false;
  var return_value;
  if (String(CallingObject) == "Sheet") {
    if (!ExecutionContext.isLhs) {
      return_value = CallingObject.getName();
      found_api_variant = true;
    }
  }
  if (CallingObject instanceof ChartInSheet) {
    if (!ExecutionContext.isLhs) {
      return_value = CallingObject.getName();
      found_api_variant = true;
    }
  }
  if (!found_api_variant) {
    ThrowException("API .name not supported yet.");
  }
  return return_value;
}

找出錯誤

如果您在轉換後的 Apps Script 程式碼中執行錯誤,該訊息會指定錯誤類型和位置。錯誤訊息的格式取決於您使用的 Apps Script 執行階段。

如果使用的是預設的 V8 執行階段,您將會看到類似下方的錯誤:

_api_windows_active (unimplemented_constructs:2:3)

這���示錯誤位於 unimplemented_constructs.gs 檔案中,第 2 行字元 (第 3 行)。

如果您在已淘汰的 Rhino 執行階段,您會看到類似下方的錯誤:

unimplemented_constructs:2 (_api_windows_active)

這表示錯誤位於第 2 行的 unimplemented_constructs.gs 檔案中。

錯誤類型

您可修正在上述 unimplemented_constructs.gsvariant_resolution.gs 檔案中遇到的大多數錯誤。

可能出現的錯誤類型包括:

尚未實作的 API

「未實作的 API」是一種 API,Macro 轉換器無法從 VBA 轉換為 Apps Script,且有該 API 沒有解決方案。

未實作的 API 通常會做為空白函式 (有時含有空白簽名) 新增至 unimplemented_constructs.gs 檔案。如果無法判定物件類型,系統可能會改為將未實作的 API 加入 variant_resolution.gs 檔案。

在轉換前產生的相容性報表中,這個 API 標示為「需要進一步調查」

如果您在轉換檔案前未在 VBA 程式碼中修正這類 API,以下是在 Apps Script 專案中顯示的情形:

/**
* Could not convert . Please add relevant code in the following
* function to implement it.
* This API has been used at the following locations in the VBA script.
*      : 
* We couldn't find an equivalent API in Apps Script for this VBA API. Please
* reconsider if this function call is critical, otherwise consider implementing
* it in a different way.
* @param param1 {}
* @param param2 {}
* ...
* @return {}
*/
function _api_(param1, param2, ....) {
  ThrowException("API  not supported yet.");
}

修正未實作的 API 錯誤

使用現有的 Apps Script API 或 JS 程式庫定義未實作的 API。步驟如下:

  1. 請在發生錯誤的位置開啟轉換後的 Apps Script 程式碼。請參閱「找出錯誤」一節。
  2. 在函式上方,讀取新增的註解。在某些情況下,您可以從註解中建議如何在 Apps Script 中實作 API。
  3. 如果在 Apps Script 中找不到實作 API 的方法,請考慮將其從程式碼中移除。
  4. 如果您找不到解決方法,或是無法從程式碼中移除這個 API,且巨集擲回這個錯誤,就無法轉換這個巨集。

未實作的 API 錯誤示例

以下是未實作的 API 情境示例及修正方式:

  • 沒有對等的 Apps Script:顯示 Chart.Protect 的間接解決方法,這是 Apps Script 中沒有的 API。
  • 不明物件類型:說明如何處理變數的物件類型,以及如何實作不支援的物件類型 (可在 Apps Script 中重新建立)。
範例 1:沒有對等的 Apps Script 或未知的 API

在此範例中,Chart.Protect 並未自動轉換,因為 Google 試算表無法保護圖表。

/**
* Could not convert chart.protect API. Please add relevant code in the following
* function to implement it.
*
* This API has been used at the following locations in the VBA script.
*     sheet1 : line 3
* You can use the following Apps Script APIs to convert it.
*
* Comments : Auto conversion of Chart.Protect is not supported yet. If the API is
* critical for the workflow the user can implement the unimplemented handler
* method in the generated code, else comment out the throw statement.
*
* @param {Object} CallingObject represents the parent object using which the API
* has been called.
* @param {string} Password
* @param {boolean} DrawingObjects
* @param {boolean} Contents
* @param {boolean} Scenarios
* @param {boolean} UserInterfaceOnly
*
*/
function _api_chart_protect(
   CallingObject, Password, DrawingObjects, Contents, Scenarios,
   UserInterfaceOnly) {
 ThrowException('API chart.protect not supported yet.');
}
雖然您無法保護圖表,但可以保護圖表的資料範圍,讓資料無法變更。

以下為保護範圍的實作範例:
/**
* Could not convert chart.protect API. Please add relevant code in the following
* function to implement it.
* This API has been used at the following locations in the VBA script.
*     sheet1 : line 3
*
* You can use the following Apps Script APIs to convert it.
* Comments : Auto conversion of Chart.Protect is not supported yet. If the API
* is critical for the workflow the user can implement the unimplemented handler
* method in the generated code, else comment out the throw statement.
*
* @param {Object} CallingObject represents the parent object using which the API
* has been called.
* @param {string} Password
* @param {boolean} DrawingObjects
* @param {boolean} Contents
* @param {boolean} Scenarios
* @param {boolean} UserInterfaceOnly
*/
function _api_chart_protect(
  CallingObject, Password, DrawingObjects, Contents, Scenarios, UserInterfaceOnly) {
var ranges = CallingObject.getChart().getRanges();
for (var i = 0; i < ranges.length; i++) {
  // Note that this does not lock the range for the document owner.
  ranges[i].protect();
}
}
示例 2:不支援的物件類型

當物件類型不明時,未實作的 API 錯誤就會新增至 variant_resolution.gs 檔案。以下範例以上述 VBA name() API 範例擴充。請見 variant_resolution.gs

在這個範例中,您會瞭解到:

  1. 如何將 name() API 轉換為 variant_resolution.gs 檔案中的新函式
  2. 如何在轉換的程式碼中呼叫新函式
  3. 如何在 Apps Script 中為 CommandBar (不支援的物件類型) 建立解決方法

1. 轉換的程式碼無法判斷呼叫 name() 的確切物件類型,因此巨集轉換工具會建立名為 __handle_resolve_name 的新函式,如下所示。

function __handle_resolve_name(ExecutionContext, CallingObject, params_map) {
 var found_api_variant = false;
 var return_value;
  if (String(CallingObject) == "Sheet") {
    if (!ExecutionContext.isLhs) {
      return_value = CallingObject.getName();
      found_api_variant = true;
    }
  }
  if (CallingObject instanceof ChartInSheet) {
    if (!ExecutionContext.isLhs) {
      return_value = CallingObject.getName();
      found_api_variant = true;
    }
  }
  if (!found_api_variant) {
    ThrowException('API .name not supported yet.');
  }
  return return_value;
}

2. 假設 VBA 程式碼定義了一個 PrintName() 函式來呼叫 name() API。VBA 程式碼如下所示:

‘Defining a function that prints the name of the object in parameter
Sub PrintName(obj as Variant)
  Debug.Print obj.Name
End Sub
由於系統會在變數物件上呼叫「name()」,因此轉換的程式碼不會在轉換時知道物件類型。轉換後的 Apps Script 程式碼會呼叫「__handle_resolve_name」函式:
function PrintName(obj) {
  Logger.log(_handle_resolve_name(obj));
}

3. 假設 VBA 程式碼對物件類型 CommandBar 呼叫 PrintName() 函式。VBA 程式碼如下所示:

PrintName Application.CommandBars.item("Standard")
Apps Script 不支援 CommandBar,因此也不支援上述 VBA 程式碼中使用的兩個方法。
  • Application.CommandBars():在 VBA 中,這會傳回所有 CommandBar 物件的清單。
  • CommandBars.item():在 VBA 中,這會傳回特定的 CommandBar 物件。
由於 Apps Script 不支援此物件類型,轉換後的程式碼會在您必須定義的「unimplementationed_建構.gs」檔案中建立下列函式。
  • _api_application_commandbars()
  • _api_commandbars_item()
系統會在轉換後的程式碼中呼叫這些函式,如下所示:
PrintName(_api_commandbars_item(_api_application_commandbars(), "Standard")))

Here’s how the new functions are added to the unimplemented_construct.gs file:

function _api_application_commandbars(CallingObject) {
  ThrowException('API application.commandbars not supported yet.');
}
function _api_commandbars_item(CallingObject, index) {
  ThrowException('API commandbars.item not supported yet.');
}

如要讓新函式順利運作,請按照下列步驟操作:

3.1 定義新的物件類型,用於建立 CommandBars 的功能和一組與 VBA 中類似的新 CommandBars 集合。

3.2 為新物件類型新增 getName() 方法。

步驟 3.1 和 3.2 如下程式碼所示。選單物件會建立做為新物件類型來模擬 CommandBars 的行為。

// Our Implementation of CommandBar using Menu objects.

function CommandBar(name) {
  this.name = name;
  // Create a menu object to represent the commandbar.
  this.menu = SpreadsheetApp.getUi().createMenu(name);
  // Create methods for retrieving or updating the name of the object
  this.getName = function() {
    return this.name;
  };
  this.updateName = function(name) {
    this.name = name;
  };
  // ========================================================================
  // Implement other methods of CommandBar objects that are used in the script.
  // =====================================================================
  return this;
}
// Our implementation of the collection of CommandBars that exists in VBA
function CommandBars() {
  this.commandBars = [];
  this.getCommandBar = function(name) {
    for (var i = 0; i < this.commandBars.length; i++) {
      if (!this.commandBars[i].getName() == name) {
        return this.commandBars[i];
      }
    }
    // No commandBar with the name exists, create a new one and return.
    var commandBar = new CommandBar(name);
    this.commandBars.push(commandBar);
    return commandBar;
  };
  return this;
}
// Create a global object that represents CommandBars collection.
var GlobalCommandBars = new CommandBars();

3.3 修改 variant_resolution.gs 檔案中的 __handle_resolve_name 函式,以便處理新的物件類型。在函式中新增區段,如下所示:

function __handle_resolve_name(ExecutionContext, CallingObject, params_map) {
 var found_api_variant = false;
 var return_value;
 if (String(CallingObject) == "Sheet") {
   if (!ExecutionContext.isLhs) {
     return_value = CallingObject.getName();
     found_api_variant = true;
   }
 }
 if (CallingObject instanceof ChartInSheet) {
   if (!ExecutionContext.isLhs) {
     return_value = CallingObject.getName();
     found_api_variant = true;
   }
 }
 // New section added below
 // ========================================================================
 if (CallingObject instanceof CommandBar) {
   objectExtend(params_map, {VALUETOSET: params_map.param0});
   if (ExecutionContext.isLhs) {
     // Call the setter method.
     CallingObject.updateName(params_map.VALUETOSET);
     found_api_variant = true;
   } else {
     // Getter is called, return the commandbar name,
     return_value = CallingObject.getName();
     found_api_variant = true;
   }
 }
 // ========================================================================
 // New section added above
 if (!found_api_variant) {
   ThrowException('API .name not supported yet.');
 }
 return return_value;
}

3.4 定義在 unimplemented_constructs.gs 檔案 (_api_application_commandbars()_api_commandbars_item()) 中建立的兩個函式。這個步驟可確保函式的原始呼叫正常運作。

//This is straightforward based on the implementation of a CommandBar and the
// CommandBars collection above:
function _api_application_commandbars(CallingObject) {
 return GlobalCommandBars;
}
function _api_commandbars_item(CallingObject, index) {
 return CallingObject.getCommandBar(index);
}

未實作的語言結構

「建構」construct是程式碼語言的元素,可控制執行流程或資料顯示方式。例如迴圈、標籤、事件和 getos。查看完整的 VBA 建構清單

系統會將巨集轉換器無法轉換的建構視為「未實作的語言結構」

如果巨集轉換工具判定已有未實作的語言結構,就會插入 TODO 註解。

系統不支援下列 VBA 結構:

修正未實作的語言建構錯誤

  1. 請更新程式碼,避免邏輯依賴不支援的語言結構。
  2. 請在發生錯誤的位置開啟轉換後的 Apps Script 程式碼。請參閱「找出錯誤」一節。
  3. 根據程式碼的邏輯,以不需要不支援語言結構的方式更新。
  4. 如果找不到他人在不支援語言結構的情況下重寫程式碼,就無法轉換這個巨集。

未實作語言建構錯誤的範例

GoTo 陳述式是最常見的未實作語言結構之一。您可以將部分 VBA GoTo 陳述式替換為迴圈。以下是兩個使用迴圈的範例,而非 GoTo 陳述式。

範例 1:將 GoTo 替換為 While Loop

原始 VBA 程式碼
Sub Test()
 a = 0
 start: Debug.Print a
 While a < 100
   a = a + 1
   If a Mod 3 == 0
     Goto start
   End If
 Wend
End Sub
對等的 Apps Script 程式碼
function test() {
 var a = 0;
 start: do {
   console.log(a);
   while (a < 100) {
     a = a + 1;
     if (a % 3 == 0) {
       continue start;
     }
   }
   break start;
 } while (true);
}

範例 2:以圓環取代 GoTo

原始 VBA 程式碼
Sub Test()
 a = 0
 For i = 1 to 100
   For j = 1 to 10
     a =a a + 1
     If i + j > 50
       GoTo endLoop
     End If
   Next j
 Next i
 endLoop: MsgBox a
End Sub
對等的 Apps Script 程式碼
function test() {
 var a = 0;
 endLoop: for (var i = 1; i <= 100; i++) {
    for  (var j = 0; j <=10; j++) {
      If (i + j > 50) {
        break endLoop;
      }
    }
 }
 Browser.msgBox(a);
}

   break start;
 } while (true);
}

部分支援的 API

針對部分支援的 API,Apps Script 支援部分輸入參數,有些則不支援。

舉例來說,VBA API legend_position 的用途是在 Excel 圖表中定義圖例。這項功能支援多種輸入值,包括:

  • xlLegendPositionBottom:在圖表底部放置圖例。
  • xlLegendPositionCorner:在圖表角落放置圖例。
  • xlLegendPositionCustom:在圖表上的自訂位置放置圖例。

Apps Script 具有對等的程式碼僅支援其中某些值。不支援下列值:

  • xlLegendPositionCorner
  • xlLegendPositionCustom

如要在轉換程式碼中標記部分支援 API 的不支援值,系統會在 library.gs 檔案中加入驗證條件,以檢查這些值。例如:

if (position == xlLegendPositionCorner ||
     position == xlLegendPositionCustom) {
   position = _handle_legend_position_error(position);
}

如果驗證條件找到其中一個不支援的值,系統就會在 unimplemented_constructs.gs 檔案中建立錯誤處理常式函式 _handle_<API_name>_error

此函式會擲回使用者錯誤,但不會將該值替換為支援的值。例如:

/**
* Throw error message for unsupported legend position.
* The VBA API Legend.Position which can take values xlLegendPositionTop,
* xlLegendPositionLeft, xlLegendPositionBottom, xlLegendPositionRight,
* xlLegendPositionCorner, xlLegendPositionCustom. It is partially supported in
* Apps Scripts that supports only a subset of the values (does not support
* xlLegendPositionCorner and xlLegendPositionCustom).
* @param {string} position
*/
function _handle_legend_position_error(position) {
// Please comment the throw statement and return a supported position value
// instead.
// Values that are supported here are xlLegendPositionTop,
// xlLegendPositionLeft, xlLegendPositionBottom, xlLegendPositionRight.
throw new Error(
   'Google Sheets does not support legend position: ' + position);
}

修正部分支援的 API 錯誤

定義 _handle_<API_name>_error 函式,將不支援的值替換為您所需的解決方法。

  1. 請在發生錯誤的位置開啟轉換後的 Apps Script 程式碼。請參閱「找出錯誤」一節。
  2. 閱讀函式上方的註解,瞭解支援和不支援的值。
  3. 針對不支援的值,判斷哪些支援值可做為適當的替換值。
  4. 更新 _handle_<API_name>_error 函式以傳回支援的值。
  5. 如果您找不到方法來取代不支援的值,就無法轉換這個巨集。

部分支援的 API 錯誤示例

以下範例以上述 VBA API legend_position 為基礎擴充內容。請參閱部分支援的 API

以下是使用不支援值 xlLegendPositionCustom 的原始 VBA 程式碼範例。

Charts(1).Legend.Position = xlLegendPositionCustom

巨集轉換工具會將下列函式新增至 unimplemented_constructs.gs 檔案:

/**
* Throw error message for unsupported legend position.
* The VBA API Legend.Position which can take values xlLegendPositionTop,
* xlLegendPositionLeft, xlLegendPositionBottom, xlLegendPositionRight,
* xlLegendPositionCorner, xlLegendPositionCustom. It is partially supported in
* Apps Scripts that supports only a subset of the values (does not support
* xlLegendPositionCorner and xlLegendPositionCustom).
* @param {string} position
*/
function _handle_legend_position_error(position) {
// Please comment the throw statement and return a supported position value
// instead.
// Values that are supported here are xlLegendPositionTop,
// xlLegendPositionLeft, xlLegendPositionBottom, xlLegendPositionRight.
throw new Error(
   'Google Sheets does not support legend position: ' + position);
}

需人工作業

「需要手動工作」表示您可以將 VBA API 轉換為 Apps Script,但需要替代方案。

在轉換前產生的相容性報表中,這類 API 會標示為「支援並具備解決方法」

如果您在轉換檔案前未在 VBA 程式碼中修正這類 API,以下是在 Apps Script 專案中顯示的情形:

/**
* Could not convert  API. Please add relevant code in the following
* function to implement it.
* This API has been used at the following locations in the VBA script.
*      : 
*
* You can use the following Apps Script APIs to convert it.
* Apps Script APIs : 
* Apps Script documentation links : 
*
* @param param1 {}
* @param param2 {}
* ...
* @return {}
*/
function _api_(param1, param2, ....) {
 ThrowException("API  not supported yet.");
}

修正需要人工操作的錯誤

實作 API 的解決方法,讓 API 正常運作。 1. 請在發生錯誤的位置開啟轉換後的 Apps Script 程式碼。請參閱「找出錯誤」一節。1. 閱讀函式上方的註解,瞭解哪些 API 可用於解決方法。1. 如果找不到適合的解決方法,請考慮從程式碼中移除 API。 1. 如果您找不到解決方法,或是從程式碼中移除這個 API,且巨集 擲回錯誤,就無法轉換這個巨集。

需要人工作業的錯誤示例

以下是擲回「手動工作必要錯誤」及「修正方式」的 API 範例:

範例 1:Autocorrect.Addreplacement

在以下範例中,可以轉換 VBA API Autocorrect.Addreplacement,但需要替代方案。巨集轉換工具會建議如何在程式碼註解中實作此函式。

/**
* Could not convert autocorrect.addreplacement API. Please add relevant code in
* the following function to implement it.
* This API has been used at the following locations in the VBA script.
*     sheet1 : line 3
* You can use the following Apps Script APIs to convert it.
* Apps Script APIs : FindReplaceRequest , onEdit
* Apps Script documentation links :
* https://developers.google.com/apps-script/reference/script/spreadsheet-trigger-builder#onedit
* https://developers.google.com/sheets/api/eap/reference/rest/v4/spreadsheets/request?hl=en#findreplacerequest

* Comments : AutoCorrect.AddReplacement was not converted, but there is an
* equivalent option you can implement manually. Use onEdit and FindReplaceRequest
* APIs instead, see https://developers.google.com/apps-script/reference/script/spreadsheet-trigger-builder#onedit
* and https://developers.google.com/sheets/api/eap/reference/rest/v4/spreadsheets/request?hl=en#findreplacerequest.
* For more information on API manual implementation, see
* https://developers.google.com/apps-script/guides/macro-converter/fix-conversion-errors.

* @param {Object} CallingObject represents the parent object using which the API
* has been called.
* @param {string} What
* @param {string} Replacement
* @return {string}
*/

function _api_autocorrect_addreplacement(CallingObject, What, Replacement) {
  ThrowException('API autocorrect.addreplacement not supported yet.');

}

Autocorrect.Addreplacement API 的實作方式如下:

var AUTO_CORRECTIONS = "AUTO_CORRECTIONS";
// Need to get the autocorrections set in previous sessions and use them.
var savedAutoCorrections = PropertiesService.getDocumentProperties().getProperty(AUTO_CORRECTIONS);
var autoCorrections = savedAutoCorrections ? JSON.parse(savedAutoCorrections) : {};
function onEdit(e) {
autoCorrect(e.range);
}
function autoCorrect(range) {
for (key in autoCorrections) {
// Replace each word that needs to be auto-corrected with their replacements.
range.createTextFinder(key)
.matchCase(true)
.matchEntireCell(false)
.matchFormulaText(false)
.useRegularExpression(false)
.replaceAllWith(autoCorrections[key]);
}
}
/**
* Could not convert autocorrect.addreplacement API. Please add relevant code in
* the following function to implement it.
* This API has been used at the following locations in the VBA script.
* sheet1 : line 3
*
* You can use the following Apps Script APIs to convert it.
* Apps Script APIs : createTextFinder , onEdit
* Apps Script documentation links : https://developers.google.com/apps-script/reference/script/spreadsheet-trigger-builder#onedit ,
createTextFinder
* Comments : AutoCorrect.AddReplacement was not converted, but there is an
* equivalent option you can implement manually. Use onEdit and FindReplaceRequest
* APIs instead, see https://developers.google.com/apps-script/reference/script/spreadsheet-trigger-builder#onedit
* and createTextFinder. For more information on API manual implementation, see
* https://developers.google.com/apps-script/guides/macro-converter/fix-conversion-errors.
*
* @param {Object} CallingObject represents the parent object using which the API has been called.
* @param {string} What
* @param {string} Replacement
*
* @return {string}
*/

function _api_autocorrect_addreplacement(CallingObject, What, Replacement) {
autoCorrections[What] = Replacement;
// Store the updated autoCorrections in the properties so that future executions use the correction.
PropertiesService.getDocumentProperties().setProperty(AUTO_CORRECTIONS, JSON.stringify(autoCorrections));
}

範例 2:Workbook.open()

VBA API workbook.open() 會根據檔案路徑開啟本機檔案。

假設 VBA 程式碼中 workbook.open() 開啟了兩個檔案:

  • 檔案 1:C:\Data\abc.xlsx
  • 檔案 2:C:\Data\xyz.xlsx

以下展示了在使用 Workbook.open() 開啟檔案 1 的地方,巨集轉換器如何將 Workbook.open() 替換為 Apps Script:

var spreadSheetId =
   _handle_mso_excel_get_google_spreadsheet_id("C:\Data\abc.xlsx");
var spreadSheet = SpreadsheetApp.openById(spreadSheetId);
以下錯誤會新增到 Apps Script 專案的 unimplemented_constructs.gs 檔案中:
/**
* Method to return the spreadsheet id manually.
*
* @param {string} FileName ID of the spreadsheet to be opened.
* @return {string} return the spreadsheet id.
*/
function _handle_mso_excel_get_google_spreadsheet_id(FileName) {
 // Upload the Excel files being opened by the API to Google Drive and convert
 // them to Google Sheets.
 // Determine the spreadsheet ID of the Google Sheets file created.
 // Implement this method to return the corresponding spreadsheet ID when given
 //the original file path as parameter.
 throw new Error('Please return the spreadsheet ID corresponding to filename: ' + FileName);
 return '';
}

如上述範例的註解所述,您需要將 Google 雲端硬碟中的目標檔案轉換成 Google 試算表檔案。

對應的 Google 試算表 ID 將以粗體顯示:

  • 檔案 #1:C:\Data\abc.xlsx 會變為 https://docs.google.com/spreadsheets/d/abc123Abc123Abc123abc
  • 檔案 #2:C:\Data\abc.xlsx 會變為 https://docs.google.com/spreadsheets/d/xyz456Xyz456xYz456xyZ

接著,修改 Apps Script 函式中的程式碼,根據 ID 開啟檔案,如下所示:

/**
* Method to return the spreadsheet id manually.
*
* @param {string} FileName ID of the spreadsheet to be opened.
* @return {string} return the spreadsheet id.
*/
function _handle_mso_excel_get_google_spreadsheet_id(FileName) {
 // Upload the Excel files being opened by the API to Google Drive and convert
 //them to Google Sheets.
 // Determine the spreadsheet ID of the Google Sheets file created.
 // Implement this method to return the corresponding spreadsheet ID when given
 //the original file path as parameter
 if (Filename.indexOf("abc.xlsx") >= 0) {
   return "abc123Abc123Abc123abc";
 } else if (Filename.indexOf("xyz.xlsx") >= 0) {
   return "xyz456Xyz456xYz456xyZ";
 }

蓄意錯誤

系統會在轉換的程式碼中加入意圖錯誤,以模擬原始 VBA 程式碼的錯誤行為。您不需要修改這些錯誤。

蓄意錯誤示例

如果您嘗試存取超出 VBA 陣列邊界的元素,程式碼就會擲回例外狀況。在 Apps Script 中,此程式碼會傳回未定義。

為了避免非預期的結果,巨集轉換工具會新增 Apps Script 程式碼,如果您嘗試存取超出陣列邊界的元素,就會擲回例外狀況。

範例如下列程式碼所示:

原始 VBA 程式碼
Dim arr
arr = Array("apple", "orange")
MsgBox arr(5)
Will throw the following error:
Subscript out of range
轉換的 Apps Script 程式碼 (新增例外狀況錯誤前)
var arr;
arr = ["apple", "orange"];
Browser.msgBox(arr[5]);
Will return this value and not throw an error:
undefined
新增會擲回例外狀況錯誤的 Apps Script 程式碼
/**
* Extend the regular JS array to support VB style indexing with a get method.
* @returns{*} value at the index
*/
Array.prototype.get = function() {
 var curr_res = this;
 for (var i = 0; i < arguments.length; i++) {
   if (!Array.isArray(curr_res) || curr_res.length < arguments[i]) {
     throw new Error(‘Converted VBA Error (Intentional Error): Subscript out of range’);
   }
   curr_res = curr_res[arguments[i]];
 }
 return curr_res;
};
var arr;
arr  = ["apple", "orange"];
Browser.msgBox(arr.get(5));