अपने बदले गए कोड की गड़बड़ियां ठीक करना

मैक्रो कन्वर्टर ऐड-ऑन, कन्वर्ज़न की ज़्यादातर ��्रोसेस को अपने-आप पूरा कर देता है. हालांकि, अपने कोड को पूरा करने के लिए, आपको कुछ एपीआई और अन्य आइटम में बदलाव करने पड़ सकते हैं.

अपने प्रोजेक्ट में जोड़ी गई Apps Script फ़ाइलों (GS फ़ाइलों) को समझने के लिए, इस गाइड का इस्तेमाल करें. साथ ही, अलग-अलग तरह की गड़बड़ियों को समझें और उन्हें ठीक करने का तरीका जानें.

अपने प्रोजेक्ट में जोड़ी गई Apps Script फ़ाइलों के बारे में जानना

इन चीज़ों में मदद करने के लिए, आपके Apps Script प्रोजेक्ट में अन्य GS फ़ाइलें जोड़ी जाती हैं:

  • उन VBA कॉन्सटेंट और वैल्यू को तय करें जो Apps Script में मौजूद नहीं हैं.
  • ऐसे एपीआई लागू करें जो ग्राहक में नहीं बदले हैं.
  • वैरिएंट से जुड़ी समस्याओं को हल करें.

आपके Apps Script प्रोजेक्ट में, ये GS फ़ाइलें जोड़ी गई हैं:

  • 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 फ़ाइल ऐसे कंस्ट्रक्शन या एपीआई के पते बनाती है जिन्हें मैक्रो कन्वर्टर की मदद से बदला नहीं जा सका. कोड को सही तरीके से काम करने के लिए, आपको इस फ़ाइल में बदलाव करना होगा.

उदाहरण: Window.Activate()

यहां Window.Activate() नाम के काम न करने वाले एपीआई का उदाहरण दिया गया है. मैक्रो कन्वर्टर, मिलते-जुलते नाम वाला एक नया Apps Script फ़ंक्शन बनाता है और उसे unimplemented_constructs.gs फ़ाइल में बताता है. VBA फ़ंक्शन काम नहीं करता है इसलिए, Apps Script का नया फ़ंक्शन एक अपवाद देता है.

नए फ़ंक्शन क��� बदली गई Apps Script कोड में उन सभी जगहों पर जोड़ दिया जाता है जहां VBA कोड में ओरिजनल एपीआई का इस्तेमाल किया गया हो.

अगर आपको ओरिजनल एपीआई के व्यवहार को फिर से बनाने का कोई तरीका मिलता है, तो आपको सिर्फ़ 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 प्रोजेक्ट में जोड़ दिया जाता है. ऐसा कई वजहों से हो सकता है, जैसे कि ऐसा एपीआई जिसमें कई रिटर्न टाइप हों या ऑब्जेक्ट का एलान अपने-आप एक वैरिएंट के तौर पर किया गया हो.

मैक्रो कन्वर्टर, इस फ़ाइल में __handle_resolve_<api>() नाम का एक नया फ़ंक्शन जोड़ता है. यह फ़ंक्शन, बताए गए एपीआई की जगह लेता है और ऑब्जेक्ट टाइप तय करने में मदद करता है.

कुछ मामलों में, आपको मैन्युअल तरीके से ऑब्जेक्ट टाइप का एलान करने के लिए, __handle_resolve_<api>() फ़ंक्शन को अपडेट करना पड़ सकता है. काम न करने वाला ऑब्जेक्ट टाइप देखें.

उदाहरण: name()

VBA में कई ऑब्जेक्ट टाइप, name() एपीआई को तय करते हैं. आम तौर पर, Apps Script getName() के बराबर होती है, लेकिन हर ऑब्जेक्ट टाइप के लिए नहीं. कई वैकल्पिक मामले हो सकते हैं:

  • ऑब्जेक्ट के बराबर वाले एपीआई को getName() से अलग कहा ज��ता है.
  • ऑब्जेक्ट का नाम पाने के लिए, उसके पास Apps Script API नहीं है.
  • Apps Script ऑब्जेक्ट में, इसके जैसा कोई ऑब्जेक्ट नहीं है.

जब ऑब्जेक्ट का टाइप तय नहीं होता है, तब मैक्रो कन्वर्टर, variant_resolutions.gs फ़ाइल में __handle_resolve_name नाम का एक नया फ़ंक्शन बनाता है.

यहां कोड में उदाहरण दिया गया है:

मूल VBA कोड

a = Selection.name

इस मामले में, मौजूदा विकल्प पर एपीआई name() को कॉल किया जाता है. चुना गया डेटा, शीट ऑब्जेक्ट या आकार ऑब्जेक्ट हो सकता है. अगर यह शीट ऑब्जेक्ट है, तो इसका मतलब है कि getName() का अनुवाद है, लेकिन अगर यह आकार वाला ऑब्जेक्ट है, तो 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)

इसका मतलब है कि गड़बड़ी unimplemented_constructs.gs फ़ाइल में लाइन 2 में मौजूद है.

गड़बड़ी के टाइप

ऊपर बताई गई unimplemented_constructs.gs और variant_resolution.gs फ़ाइलों में हुई ज़्यादातर गड़बड़ियां ठीक की जा सकती हैं.

आपको इस तरह की गड़बड़ियों का सामना करना पड़ सकता है:

लागू नहीं किया गया एपीआई

लागू नहीं किया गया एपीआई एक ऐसा एपीआई है जिसे मैक्रो कन्वर्टर, VBA से Apps Script में नहीं बदल सकता. साथ ही, इस एपीआई का कोई कारगर तरी���ा उपलब्ध नहीं है.

जो एपीआई लागू नहीं किए गए हैं उन्हें unimplemented_constructs.gs फ़ाइल में आम तौर पर खाली फ़ंक्शन के तौर पर जोड़ा जाता है. कभी-कभी ऐसे एपीआई को खाली हस्ताक्षर के साथ भी जोड़ा जाता है. अगर ऑब्जेक्ट टाइप तय नहीं हो पा रहा है, तो इसकी जगह, उस एपीआई को variant_resolution.gs फ़ाइल में जोड़ा जा सकता है जिसे लागू नहीं किया गया है.

कन्वर्ज़न से पहले जनरेट की गई, काम करने की रिपोर्ट में इस एपीआई को ज़्यादा जांच की ज़रूरत है के तौर पर लेबल किया गया है.

अगर अपनी फ़ाइल को बदलने से पहले आप अपने VBA कोड में इस तरह के एपीआई को ठीक नहीं करते हैं, तो 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.");
}

लागू नहीं की गई एपीआई की गड़बड़ियां ठीक करना

मौजूदा Apps Script API या JS लाइब्रेरी के साथ, लागू नहीं किए गए एपीआई के बारे में बताएं. ऐसा करने के लिए, यह तरीका अपनाएं:

  1. गड़बड़ी वाली जगह पर बदला गया Apps Script कोड खोलें. गड़बड़ियां ढूंढना देखें.
  2. फ़ंक्शन के ऊपर, जोड़ी गई टिप्पणी पढ़ें. कुछ मामलों में, टिप्पणी से पता चलता है कि Apps Script में एपीआई को कैसे लागू किया जाए.
  3. अगर आपको Apps Script में एपीआई लागू करने का कोई तरीका नहीं मिल रहा है, तो इसे अपने कोड से हटाएं.
  4. अगर आपको कोई समाधान नहीं मिलता या आपको अपने कोड से यह एपीआई नहीं मिल रहा और आपके मैक्रो में यह गड़बड़ी दिखती है, तो आप इस मैक्रो को नहीं ��दल सकते.

लागू नहीं की गई एपीआई की गड़बड़ियों के उदाहरण

यहां एपीआई की ऐसी स्थितियों के उदाहरण दिए गए हैं जिन्हें लागू नहीं किया गया है और उन्हें ठीक करने का तरीका भी बताया गया है:

  • इसके बराबर कोई Apps Script नहीं है: यह Chart.Protect के लिए, सीधे तौर पर काम न करने वाला कोई विकल्प दिखाता है. यह एक ऐसा एपीआई है जो Apps Script में मौजूद नहीं है.
  • एक ऐसा ऑब्जेक्ट टाइप जिसकी जानकारी नहीं है: इसमें, ऐसे ऑब्जेक्ट टाइप को मैनेज करने का तरीका बताया जाता है जो वैरिएबल में होता है. साथ ही, इसमें ऐसे ऑब्जेक्ट टाइप को लागू करने का तरीका बताया जाता है जो Apps Script में फिर से बनाया जा सकता है.
उदाहरण 1: मिलती-जुलती कोई Apps Script या अज्ञात एपीआई

इस उदाहरण में, Chart.Protect अपने-आप नहीं बदला गया, क्योंकि Google Sheets में चार्ट को सुरक्षित रखने का कोई तरीका नहीं था.

/**
* 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();
}
}
दूसरा उदाहरण: ऑब्जेक्ट का टाइप इस्तेमाल नहीं किया जा सकता

जब ऑब्जेक्ट टाइप की जानकारी नहीं होती है, तब variant_resolution.gs फ़ाइल में, लागू नहीं की गई एपीआई गड़बड़ी जोड़ दी जाती है. इस उदाहरण में, ऊपर दिए गए VBA name() एपीआई के उदाहरण के बारे में ज़्यादा जानकारी दी गई है. variant_resolution.gs देखें.

इस उदाहरण में आपको यह जानकारी मिलेगी:

  1. name() एपीआई को 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 को कॉल करता है. वीबीए कोड नीचे दिखाया गया है:

‘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() फ़ंक्शन को कॉल करता है. वीबीए कोड नीचे दिखाया गया है:

PrintName Application.CommandBars.item("Standard")
Apps Script में CommandBar काम नहीं करता. इसलिए, ऊपर दिए गए VBA कोड में इस्तेमाल किए गए दो तरीके भी काम नहीं करते.
  • Application.CommandBars(): VBA में यह सभी CommandBar ऑब्जेक्ट की सूची दिखाता है.
  • CommandBars.item(): VBA में यह एक खास CommandBar ऑब्जेक्ट दिखाता है.
यह ऑब्जेक्ट टाइप, Apps Script में इस्तेमाल नहीं किया जा सक��ा. इसलिए, बदला गया कोड `unलागूed_constructs.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, कोड लैंग्वेज का एक ऐसा एलिमेंट है जो एक्ज़ीक्यूशन फ़्लो या डेटा डिसप्ले को कंट्रोल करता है. उदाहरण के लिए, लूप, लेबल, इवेंट, और गॉटो. यहां सभी VBA कंस्ट्रक्ट की सूची दी गई है.

जिन कंस्ट्रक्शन को मैक्रो कन्वर्टर में बदला नहीं जा सकता उन्हें लागू नहीं की गई भाषा के कंस्ट्रक्शन माना जाता है.

मैक्रो कन्वर्टर जब यह तय करता है कि कोई ऐसी लैंग्वेज कंस्ट्रक्शन मौजूद है जिसे लागू नहीं किया गया है, तो वहां एक TODO टिप्पणी डाल दी जाती है.

ये VBA कंस्ट्रक्ट काम नहीं करते:

लागू नहीं की गई भाषा निर्माण से जुड़ी गड़बड़ियों को ठीक करना

  1. अपना कोड अपडेट करें, ताकि आपका लॉजिक, काम न करने वाली भाषा पर निर्भर न हो.
  2. गड़बड़ी वाली जगह पर बदला गया Apps Script कोड खोलें. गड़बड़ियां ढूंढना देखें.
  3. कोड के लॉजिक के आधार पर, उसे इस तरह से अपडेट करें कि साइट पर काम न करने वाली भाषा ��नाने की ज़रूरत न पड़े.
  4. अगर आपको अपने कोड को फिर से लिखने का तरीका काम नहीं आने वाली भाषा के बिना नहीं लगता, तो आप इस मैक्रो को बदल नहीं सकते.

भाषा बनाने से जुड़ी ऐसी गड़बड़ियों के उदाहरण जिन्हें लागू नहीं किया गया है

GoTo स्टेटमेंट एक ऐसा लैंग्वेज कंस्ट्रक्शन है जिसे लागू नहीं किया गया है. कुछ VBA GoTo स्टेटमेंट को लूप से बदला जा सकता है. यहां GoTo स्टेटमेंट के बजाय लूप का इस्तेमाल करने के दो उदाहरण दिए गए हैं.

पहला उदाहरण: 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);
}

कुछ हद तक काम करने वाला एपीआई

कुछ हद तक काम करने वाले एपीआई के लिए, कुछ इनपुट पैरामीटर Apps Script में काम करते हैं और कुछ नहीं.

उदाहरण के लिए, VBA API legend_position का इस्तेमाल किसी Excel ग्राफ़ में लेजेंड तय करने के लिए किया जाता है. यह कई तरह की इनपुट वैल्यू के साथ काम करता है. इनमें ये शामिल हैं:

  • xlLegendPositionBottom: लेजेंड को चार्ट में सबसे नीचे रखता है.
  • xlLegendPositionCorner: लेजेंड को चार्ट के कोने में लगाता है.
  • xlLegendPositionCustom: लेजेंड को चार्ट पर कस्टम स्थितियों पर डालता है.

Apps Script में एक जैसा कोड मौजूद है, जो इनमें से कुछ ही वैल्यू के साथ काम करता है. ये वैल्यू इस्तेमाल नहीं की जा सकतीं:

  • xlLegendPositionCorner
  • xlLegendPositionCustom

आपके बदले गए कोड में, कुछ हद तक काम करने वाल��� ��पीआई क�� ��ाम �� कर���े ��ाली वैल्यू को फ़्लैग करने के लिए, 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);
}

कुछ हद तक काम करने वाले एपीआई की गड़बड़ियों को ठीक करना

_handle_<API_name>_error फ़ंक्शन तय करें, ताकि इस्तेमाल न की जा सकने वाली वैल्यू को अपनी ज़रूरतों के हिसाब से सही तरीके से बदला जा सके.

  1. गड़बड़ी वाली जगह पर बदला गया Apps Script कोड खोलें. गड़बड़ियां ढूंढना देखें.
  2. कौनसी वैल्यू काम करती हैं और कौनसी नहीं, यह जानने के लिए फ़ंक्शन के ऊपर दी गई टिप्पणी पढ़ें.
  3. इस्तेमाल न की जा सकने वाली वैल्यू के लिए, यह तय करें कि कौनसी वैल्यू सही रिप्लेसमेंट की तरह काम कर सकती हैं.
  4. इसके बजाय, इस्तेमाल की जा सकने वाली वैल्यू दिखाने के लिए, _handle_<API_name>_error फ़ंक्शन को अपडेट करें.
  5. अगर आपको काम न करने वाली वैल्यू को बदलने का तरीका नहीं मिल रहा है, तो इस मैक्रो को बदला नहीं जा सकता.

कुछ हद तक काम करने वाले एपीआई की गड़बड़ी का उदाहरण

यहां दिए गए उदाहरण में, ऊपर बताए गए VBA API legend_position के बारे में ��़्यादा जानकारी दी गई है. कुछ हद तक काम करने वाला एपीआई देखें.

यहां ओरिजनल 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 में बदला जा सकता है. हालांकि, इसके लिए कुछ तरीकों की ज़रूरत है.

कन्वर्ज़न से पहले जनरेट की गई कंपैटबिलिटी रिपोर्ट में, इस तरह के एपीआई को इस तरह के एपीआई के साथ काम करता है के तौर पर लेबल किया जाता है.

अगर अपनी फ़ाइल को बदलने से पहले आप अपने VBA कोड में इस तरह के एपीआई को ठीक नहीं करते हैं, तो 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.");
}

मैन्युअल काम की आवश्यक त्रुटियां ठीक करें

एपीआई के लिए कोई समाधान लागू करें, ताकि एपीआई सही तरीके से काम करे. 1. गड़बड़ी वाली जगह पर बदला गया Apps Script कोड खोलें. गड़बड़ियां ढूंढना देखें. 1. यह समझने के लिए कि समाधान के लिए कौनसे एपीआई इस्तेमाल किए जा सकते हैं, फ़ंक्शन के ऊपर दी गई टिप्पणी पढ़ें. 1. अगर आपको सही तरीका नहीं मिल रहा है, तो अपने कोड से एपीआई को हटाएं. 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 एपीआई को लागू करने की प्रोसेस यहां दी गई है:

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

दूसरा उदाहरण: Workbook.open()

VBA API workbook.open(), फ़ाइल पाथ के आधार पर लोकल फ़ाइल खोलता है.

मान लीजिए कि workbook.open() की मदद से VBA कोड में दो फ़ाइलें खोली जा रही हैं:

  • फ़ाइल 1: C:\Data\abc.xlsx
  • फ़ाइल 2: C:\Data\xyz.xlsx

यहां यह दिखाया गया है कि कैसे मैक्रो कन्वर्टर, 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 Drive पर मौजूद Google Sheets फ़ाइलों में बदलना होगा.

संबंधित Google स्प्रेडशीट आईडी नीचे बोल्ड किए गए हैं:

  • फ़ाइल #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 फ़ंक्शन में कोड में बदलाव करें, जैसा कि यहां दिखाया गया है:

/**
* 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 में किसी अरे की सीमा से बाहर के किसी एलिमेंट को ऐक्सेस करने की कोशिश की जाती है, तो कोड एक अपवाद लागू करता है. Apps Script में, कोड 'तय नहीं है' के तौर पर दिखता है.

अनचाहे न��ीजों से बचने के लिए, मैक्रो कन्वर्टर, Apps Script कोड को जोड़ता है. अगर किसी अरे की सीमा से बाहर के एलिमेंट ऐक्सेस करने की कोशिश की जाती है, तो उस कोड को अपवाद के तौर पर ऐक्सेस किया जा सकता है.

यह उदाहरण नीचे दिए गए कोड में दिखाया गया है:

मूल VBA कोड
Dim 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
अपवाद से जुड़ी गड़बड़ी की जानकारी देने के लिए, 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));