O complemento Macro Converter automatiza a maior parte do processo de conversão, mas talvez seja necessário fazer ajustes em algumas APIs e outros itens para finalizar o código.
Use este guia para entender os arquivos do Apps Script (arquivos GS) adicionados ao seu projeto, interpretar os diferentes tipos de erros e aprender a corrigir erros.
Entender os arquivos do Apps Script adicionados ao seu projeto
Outros arquivos GS são adicionados ao seu projeto do Apps Script para ajudar a:
- Defina constantes e valores do VBA que não existem no Apps Script.
- Implemente APIs não convertidas.
- Resolva as variantes.
Os seguintes arquivos GS são adicionados ao seu projeto do Apps Script:
Library.gs
Unimplemented_constructs.gs
Variant_resolutions.gs
Library.gs
Em geral, não é necessário modificar nada no arquivo library.gs
.
O arquivo library.gs
define funções e constantes usadas no código VBA
que não existem no Apps Script. Isso ajuda o novo código do Apps Script a se parecer mais com o código VBA. Além disso, não é necessário repetir as definições sempre que funções ou constantes do arquivo library.gs
forem usadas.
Unimplemented_constructs.gs
O arquivo unimplemented_constructs.gs
aborda construções ou APIs que não puderam
ser convertidas pelo Macro Converter. É provável que você precise modificar esse arquivo para que
o código funcione como esperado.
Exemplo: Window.Activate()
Confira abaixo um exemplo de uma API não compatível chamada Window.Activate()
.
O Conversor de macros cria uma função do Apps Script com um nome semelhante e a
define no arquivo unimplemented_constructs.gs
. Como a função VBA
não é compatível, a nova função do Apps Script gera uma exceção.
A nova função é adicionada ao código convertido do Apps Script em todos os lugares em que a API original foi usada no código VBA.
Se você encontrar uma solução alternativa para recriar o comportamento da API original, basta
atualizar a definição da função no arquivo unimplemented_constructs.gs
. Depois que a função é definida, ela é aplicada em todos os lugares em que aparece no projeto do Apps Script.
Confira o exemplo no código:
Código VBA original
Window.activate()
Código do Apps Script convertido, adicionado inline
_api_window_activate();
Definição de função adicionada ao arquivo 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
O arquivo variant_resolutions.gs
é adicionado ao projeto do Apps Script se o tipo de um objeto não puder ser determinado. Isso pode acontecer por vários motivos, como
uma API ter vários tipos de retorno ou o objeto ser declarado como uma variante
em si.
O Macro Converter adiciona uma nova função a esse arquivo chamada __handle_resolve_<api>()
que substitui a API em questão e ajuda a determinar o tipo de objeto.
Em alguns casos, talvez seja necessário atualizar a função __handle_resolve_<api>()
para declarar manualmente o tipo de objeto. Consulte Tipo de objeto não compatível.
Exemplo: name()
Muitos tipos de objetos em VBA definem uma API name()
. Normalmente, o equivalente do Apps Script é getName()
, mas não para todos os tipos de objetos. Vários casos alternativos podem ocorrer:
- A API equivalente do objeto tem um nome diferente de
getName()
. - O objeto não tem uma API Apps Script para receber o nome.
- Não há um objeto equivalente do Apps Script.
Quando o tipo de objeto não é determinado, o conversor de macros cria uma nova
função chamada __handle_resolve_name
no arquivo variant_resolutions.gs
.
Confira o exemplo no código:
Código VBA original
a = Selection.name
Nesse caso, a API name()
é chamada na seleção atual. A seleção pode ser um objeto "Sheet" ou "Shape". Se for um objeto de planilha, a
tradução será getName()
, mas se for um objeto de forma, não haverá equivalente
no Apps Script.
Código do Apps Script convertido, adicionado inline
a = __handle_resolve_name({}, getActiveSelection(), {});
A função __handle_resolve_name()
abaixo é adicionada ao arquivo
variant_resolution.gs
para resolver diferentes tipos de objetos. A função verifica o tipo de objeto e usa getName()
se ele for compatível ou gera um erro se não for.getName()
Definição de função adicionada ao arquivo 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; }
Encontrar erros
Quando você encontra um erro no código convertido do Apps Script, a mensagem especifica o tipo de erro e o local dele. O formato da mensagem de erro depende do tempo de execução do Apps Script que você está usando.
Se você estiver no tempo de execução padrão do V8, vai aparecer um erro como este:
_api_windows_active (unimplemented_constructs:2:3)
Isso significa que o erro está localizado no arquivo unimplemented_constructs.gs
na linha 2, caractere 3.
Se você estiver no tempo de execução do Rhino descontinuado, vai aparecer um erro como este:
unimplemented_constructs:2 (_api_windows_active)
Isso significa que o erro está localizado no arquivo unimplemented_constructs.gs
na linha 2.
Tipos de erro
É possível corrigir a maioria dos erros encontrados nos arquivos unimplemented_constructs.gs
e variant_resolution.gs
descritos acima.
Os tipos de erros que você pode encontrar incluem:
- API não implementada
- Estrutura de linguagem não implementada
- API parcialmente compatível
- Trabalho manual necessário
- Erro intencional
API não implementada
Uma API não implementada é uma API que o Macro Converter não consegue converter de VBA para Apps Script e não há uma solução alternativa conhecida para ela.
As APIs não implementadas geralmente são adicionadas como funções vazias (às vezes com assinaturas vazias) ao arquivo unimplemented_constructs.gs
. Se o tipo de objeto não puder ser determinado, a API não implementada poderá ser adicionada ao arquivo variant_resolution.gs
.
No relatório de compatibilidade gerado antes da conversão, essa API é marcada como Precisa de mais investigação.
Se você não corrigir esse tipo de API no código VBA antes de converter o arquivo, ele vai aparecer assim no projeto do 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("APInot supported yet." ); }
Corrigir erros de API não implementados
Defina a API não implementada com as APIs Apps Script ou bibliotecas JS atuais. Para isso, siga estas etapas:
- Abra o código convertido do Apps Script no local do erro. Consulte Encontrar erros.
- Acima da função, leia o comentário adicionado. Em alguns casos, o comentário sugere como implementar a API no Apps Script.
- Se você não encontrar uma maneira de implementar a API no Apps Script, considere removê-la do seu código.
- Se você não conseguir encontrar uma solução alternativa ou remover essa API do seu código e sua macro gerar esse erro, não será possível converter a macro.
Exemplos de erros de API não implementados
Confira exemplos de cenários de API não implementados e como corrigir esses problemas:
- Não há um equivalente no Apps Script:
mostra uma solução alternativa indireta para
Chart.Protect
, uma API que não existe no Apps Script. - Um tipo de objeto desconhecido: mostra como processar um tipo de objeto que é uma variável e como implementar um tipo de objeto sem suporte que pode ser recriado no Apps Script.
Exemplo 1: nenhum equivalente do Apps Script ou API desconhecida
Neste exemplo, Chart.Protect
não foi convertido automaticamente porque não há como proteger um gráfico nas Planilhas 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.'); }
Confira abaixo um exemplo de implementação para proteger o intervalo:
/** * 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(); } }
Exemplo 2: tipo de objeto sem suporte
Quando o tipo de objeto é desconhecido, o erro de API não implementada é adicionado ao arquivo
variant_resolution.gs
. O exemplo a seguir expande o exemplo de API name()
em VBA acima. Consulte variant_resolution.gs
.
Neste exemplo, você vai aprender:
- Como a API
name()
é convertida em uma nova função no arquivovariant_resolution.gs
. - Como a nova função é chamada no código convertido.
- Como criar uma solução alternativa para
CommandBar
, um tipo de objeto sem suporte, no Apps Script.
1. Como o código convertido não consegue determinar o tipo exato de objeto em que name()
é chamado, o Macro Converter cria uma nova função chamada
__handle_resolve_name
, mostrada abaixo.
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. Suponha que o código VBA defina uma função PrintName()
que chama a API name()
. Confira o código VBA abaixo:
‘Defining a function that prints the name of the object in parameter Sub PrintName(obj as Variant) Debug.Print obj.Name End Sub
function PrintName(obj) { Logger.log(_handle_resolve_name(obj)); }
3. Suponha que seu código VBA chame a função PrintName()
no tipo de objeto CommandBar
. Confira o código VBA abaixo:
PrintName Application.CommandBars.item("Standard")
CommandBar
não é compatível com o Apps Script e, como resultado, os dois métodos usados no código VBA acima também não são compatíveis.
Application.CommandBars()
: em VBA, isso retorna uma lista de todos os objetosCommandBar
.CommandBars.item()
: em VBA, isso retorna um objetoCommandBar
específico.
_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.'); }
Para que as novas funções funcionem, siga estas etapas:
3.1 Defina um novo tipo de objeto que crie as funcionalidades de CommandBars
e uma nova coleção de CommandBars
semelhante ao que existe no VBA.
3.2 Adicione um método getName()
para o novo tipo de objeto.
As etapas 3.1 e 3.2 são mostradas no código abaixo. Os objetos de menu são criados como um
novo tipo de objeto que imita o comportamento de 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 Modifique a função __handle_resolve_name
no arquivo variant_resolution.gs
para processar o novo tipo de objeto. Adicione uma seção à função, conforme mostrado
abaixo:
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 Defina as duas funções criadas no arquivo unimplemented_constructs.gs
(_api_application_commandbars()
, _api_commandbars_item()
). Esta etapa garante que as chamadas originais da função funcionem.
//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); }
Construções de linguagem não implementadas
Um elemento de programação é um elemento da linguagem de programação que controla o fluxo de execução ou a exibição de dados. Por exemplo, loops, rótulos, eventos e comandos "goto". Confira uma lista de todas as construções do VBA.
As construções que o conversor de macros não consegue converter são consideradas construções de linguagem não implementadas.
Quando o conversor de macros determina que existe uma construção de linguagem não implementada, ele insere um comentário TODO
.
As seguintes construções de VBA não são compatíveis:
- AddressOf
- Declarar
- DefType
- GoSub
- GoTo
- Implementa
- Lset
- Abrir
- RaiseEvent
- Nome
- Retomar
- Rset
- TypeOf
- Turma
- Módulos de classe
Corrigir erros de construção de linguagem não implementada
- Atualize seu código para que a lógica não dependa da construção de linguagem sem suporte.
- Abra o código convertido do Apps Script no local do erro. Consulte Encontrar erros.
- Com base na lógica do código, atualize-o de forma que não exija a estrutura de linguagem sem suporte.
- Se você não conseguir reescrever o código sem o constructo de linguagem não compatível, não será possível converter essa macro.
Exemplos de erros de construção de linguagem não implementada
Uma das construções de linguagem não implementadas mais comuns é uma instrução GoTo
.
É possível substituir algumas instruções VBA GoTo
por loops. Confira abaixo dois exemplos de como usar loops em vez de instruções GoTo
.
Exemplo 1: substitua GoTo
por While Loop
Código VBA original
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
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); }
Exemplo 2: substituir GoTo por For Loop
Código VBA originalSub 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
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 parcialmente compatível
Para APIs parcialmente compatíveis, alguns parâmetros de entrada são compatíveis com o Apps Script, e outros não.
Por exemplo, a API VBA legend_position
é usada para definir a legenda em um gráfico do Excel. Ele aceita vários tipos de valores de entrada, incluindo:
xlLegendPositionBottom
: coloca a legenda na parte de baixo do gráfico.xlLegendPositionCorner
: coloca a legenda no canto do gráfico.xlLegendPositionCustom
: coloca a legenda em posições personalizadas no gráfico.
O Apps Script tem um código equivalente que aceita apenas alguns desses valores. Os valores a seguir não são compatíveis:
xlLegendPositionCorner
xlLegendPositionCustom
Para sinalizar valores não aceitos de APIs parcialmente aceitas no código convertido,
uma condição de validação é adicionada ao arquivo library.gs
, que verifica esses
valores. Exemplo:
if (position == xlLegendPositionCorner || position == xlLegendPositionCustom) { position = _handle_legend_position_error(position); }
Se a condição de validação encontrar um dos valores não aceitos, uma função
de tratamento de erros, _handle_<API_name>_error
, será criada no
arquivo unimplemented_constructs.gs
.
A função gera um erro do usuário e não substitui o valor por um valor compatível. Exemplo:
/** * 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); }
Corrigir erros de API parcialmente compatíveis
Defina a função _handle_<API_name>_error
para substituir os valores não aceitos
por uma solução alternativa aceitável para suas necessidades.
- Abra o código convertido do Apps Script no local do erro. Consulte Encontrar erros.
- Leia o comentário acima da função para entender quais valores são compatíveis e quais não são.
- Para os valores não aceitos, determine quais valores aceitos podem ser uma substituição adequada.
- Atualize a função
_handle_<API_name>_error
para retornar um valor compatível. - Se você não conseguir substituir o valor sem suporte, não será possível converter essa macro.
Exemplo de um erro de API parcialmente compatível
O exemplo a seguir detalha a API VBA legend_position
mencionada acima.
Consulte API parcialmente compatível.
Confira abaixo um exemplo de código VBA original que usa um valor sem suporte, xlLegendPositionCustom
.
Charts(1).Legend.Position = xlLegendPositionCustom
O Macro Converter adiciona a função abaixo ao arquivo 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); }
Trabalho manual necessário
Trabalho manual necessário significa que a API VBA pode ser convertida em Apps Script, mas precisa de uma solução alternativa.
No relatório de compatibilidade gerado antes da conversão, esse tipo de API é rotulado como Compatível com soluções alternativas.
Se você não corrigir esse tipo de API no código VBA antes de converter o arquivo, ele vai aparecer assim no projeto do Apps Script:
/** * Could not convertAPI. 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("APInot supported yet." ); }
Corrigir erros que exigem trabalho manual
Implemente uma solução alternativa para que a API funcione conforme o esperado. 1. Abra o código convertido do Apps Script no local do erro. Consulte Encontrar erros. 1. Leia o comentário acima da função para entender quais APIs podem ser usadas em uma solução alternativa. 1. Se não for possível encontrar uma solução alternativa adequada, remova a API do seu código. 1. Se você não conseguir encontrar uma solução alternativa ou remover essa API do seu código e sua macro gerar um erro, não será possível converter essa macro.
Exemplos de erros que exigem trabalho manual
Confira exemplos de APIs que geram erros de trabalho manual necessário e como corrigir esses erros:
Implement a workaround for Autocorrect.Addreplacement
.Implement a workaround for workbook.open()
. Este exemplo mostra como abrir arquivos no Google Drive com o Apps Script.
Exemplo 1: Autocorrect.Addreplacement
No exemplo a seguir, a API VBA Autocorrect.Addreplacement
pode ser convertida, mas precisa de uma solução alternativa. O conversor de macros sugere como implementar a função nos comentários do código.
/** * 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.'); }
A implementação da API Autocorrect.Addreplacement
é mostrada abaixo:
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)); }
Exemplo 2: Workbook.open()
A API VBA workbook.open()
abre um arquivo local com base em um caminho de arquivo.
Suponha que haja dois arquivos sendo abertos por workbook.open()
no código VBA:
- Arquivo 1:
C:\Data\abc.xlsx
- Arquivo 2:
C:\Data\xyz.xlsx
Abaixo, mostramos como o Macro Converter substitui Workbook.open()
por Apps
Script em todos os lugares em que Workbook.open()
é usado para abrir o arquivo 1:
var spreadSheetId = _handle_mso_excel_get_google_spreadsheet_id("C:\Data\abc.xlsx"); var spreadSheet = SpreadsheetApp.openById(spreadSheetId);
unimplemented_constructs.gs
no projeto do Apps Script:
/** * 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 '' ; }
Conforme instruído nos comentários do exemplo acima, você precisa converter os arquivos de destino em arquivos das Planilhas Google no Google Drive.
Os IDs correspondentes das planilhas Google estão em negrito abaixo:
- Arquivo 1:
C:\Data\abc.xlsx
se tornahttps://docs.google.com/spreadsheets/d/abc123Abc123Abc123abc
- Arquivo 2:
C:\Data\abc.xlsx
passa a serhttps://docs.google.com/spreadsheets/d/xyz456Xyz456xYz456xyZ
Em seguida, modifique o código na função do Apps Script para abrir os arquivos por ID, conforme mostrado abaixo:
/** * 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"; }
Erro intencional
Erros intencionais são adicionados ao código convertido para imitar o comportamento de erro do código VBA original. Não é necessário modificar esses erros.
Exemplo de um erro intencional
Se você tentar acessar um elemento além dos limites de uma matriz em VBA, o código vai gerar uma exceção. No Apps Script, o código retorna "undefined".
Para evitar resultados inesperados, o Conversor de macros adiciona um código do Apps Script que gera uma exceção se você tentar acessar elementos além dos limites de uma matriz.
Confira o exemplo no código abaixo:
Código VBA originalDim arr arr = Array("apple", "orange") MsgBox arr(5) Will throw the following error: Subscript out of range
var arr; arr = ["apple", "orange"]; Browser.msgBox(arr[5]); Will return this value and not throw an error: undefined
/** * 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));
Artigos relacionados
- Visão geral do complemento Macro Converter
- Determinar se as macros do VBA são compatíveis
- Converter macros do VBA para o Apps Script
- Resolver problemas comuns
- Assistir tutoriais do Macro Converter
- Lista de APIs VBA compatíveis