修正轉換程式碼中的錯誤

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

請參閱本指南,瞭解專案中新增的 Apps Script 檔案 (GS 檔案)、解讀不同類型的錯誤,以及學習如何修正錯誤。

瞭解專案中新增的 Apps Script 檔案

其他 GS 檔案會新增至 Apps Script 專案,協助您:

  • 定義 Apps Script 中不存在的 VBA 常數和值。
  • 導入未轉換的 API。
  • 解決變化版本問題。

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

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

Library.gs

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

library.gs 檔案會定義 VBA 程式碼中使用的函式和常數,但這些函式和常數不存在於 Apps Script 中。這樣一來,新的 Apps Script 程式碼就更像 VBA 程式碼。此外,每次使用 library.gs 檔案中的函式或常數時,您都不需要重複定義。

Unimplemented_constructs.gs

unimplemented_constructs.gs 檔案會列出 Macro Converter 無法轉換的建構函式或 API。您可能需要修改這個檔案,才能讓程式碼如預期運作。

範例:Window.Activate()

以下是不支援的 API 呼叫 Window.Activate() 範例。巨集轉換器會建立名稱類似的新 Apps Script 函式,並在 unimplemented_constructs.gs 檔案中定義該函式。由於系統不支援 VBA 函式,新的 Apps Script 函式會擲回例外狀況。

轉換後的 Apps Script 程式碼中,凡是使用原始 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 物件或 Shape 物件。如果是 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」是指 Macro Converter 無法將 VBA 轉換為 Apps Script 的 API,且沒有已知的 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_<API_name>(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 的間接解決方法,這個 API 不存在於 Apps Script 中。
  • 不明物件類型:說明如何處理變數物件類型,以及如何實作可在 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 程式碼會呼叫物件類型 CommandBarPrintName() 函式。VBA 程式碼如下所示:

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

Heres 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);
}

未實作的語言建構

「建構」是程式碼語言的元素,可控制執行流程或資料顯示方式。例如迴圈、標籤、事件和 goto。 如需所有 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 替換為 For 迴圈

原始 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」。

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

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_<API_name>(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

下圖顯示 Macro Converter 如何將 Workbook.open() 替換為 Apps Script,以便在所有使用 Workbook.open() 開啟檔案 1 的位置執行這項操作:

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));