تسجيل الأنشطة والأخطاء

يوضّح هذا الدليل كيفية كتابة سجلّات مخصّصة ورسائل خطأ. تظهر هذه الرسائل في علامة التبويب "النشاط" في Workspace Studio ويمكن أن تساعدك في تحديد المشاكل في الخطوات التي يتعذّر تنفيذها وحلّها.

تسجّل علامة التبويب "النشاط" تلقائيًا اسم الخطوة التي يتم تنفيذها كما هو محدّد في ملف البيان الخاص بها. لمساعدتك في فهم ما حدث أثناء تنفيذ خطوة، عليك أيضًا كتابة سجلّات مخصّصة للخطوة. إذا واجه المستخدمون سلوكًا غير متوقّع أثناء تنفيذ خطوتك، يمكن أن تساعدهم السجلات في فهم ما حدث.

يتضمّن إدخال في السجلّ المفيد سمتَين:

  • شريحة تحتوي على رابط تشعّبي إلى المرجع الذي تم إنشاؤه أو تعديله بواسطة الخطوة. على سبيل المثال، إذا كانت خطوتك تنشئ مستند Google، استخدِم الشريحة للربط بمستند Google الذي تم إنشاؤه.
  • رسالة خطأ مفصّلة توضّح سبب تعذُّر تنفيذ إحدى الخطوات وكيفية حلّ المشكلة

إنشاء سجلّ أنشطة

يوضّح نموذج الرمز التالي كيف يمكن onExecuteFunctionCreateDocument تسجيل عملية تشغيل ناجحة وخطأ في علامة التبويب "النشاط".

في ما يلي ملف البيان:

JSON

{
  "timeZone": "America/Los_Angeles",
  "exceptionLogging": "STACKDRIVER",
  "runtimeVersion": "V8",
  "addOns": {
    "common": {
      "name": "Log and Error Demo",
      "logoUrl": "https://www.gstatic.com/images/branding/productlogos/gsuite_addons/v6/web-24dp/logo_gsuite_addons_color_1x_web_24dp.png",
      "useLocaleFromApp": true
    },
    "flows": {
      "workflowElements": [
        {
          "id": "log_and_error_demo",
          "state": "ACTIVE",
          "name": "Log and Error Demo",
          "description": "Display a log message when executed successfully, display an error message and retry execution instead.",
          "workflowAction": {
            "inputs": [
              {
                "id": "value1",
                "description": "value1",
                "cardinality": "SINGLE",
                "dataType": {
                  "basicType": "INTEGER"
                }
              }
            ],
            "outputs": [
              {
                "id": "result",
                "description": "execution result",
                "cardinality": "SINGLE",
                "dataType": {
                  "basicType": "STRING"
                }
              }
            ],
            "onConfigFunction": "onConfigFunctionCreateDocument",
            "onExecuteFunction": "onExecuteFunctionCreateDocument"
          }
        }
      ]
    }
  }
}

في ما يلي رمز منطق التطبيق:

برمجة التطبيقات

function onConfigFunctionCreateDocument() {
  const firstInput = CardService.newTextInput()
    .setFieldName("value1")
    .setTitle("First Value") //"FieldName" must match an "id" in the manifest file's inputs[] array.
    .setHint("Enter 1 to successfully execute the step, 0 to fail the step and return an error.")
    .setHostAppDataSource(
      CardService.newHostAppDataSource()
        .setWorkflowDataSource(
          CardService.newWorkflowDataSource()
            .setIncludeVariables(true)
        )
    );

  let cardSection = CardService.newCardSection()
    .addWidget(firstInput);

  return CardService.newCardBuilder()
    .addSection(cardSection)
    .build();
}

function onExecuteFunctionCreateDocument(event) {

  // true if the document is successfully created, false if something goes wrong.
  var successfulRun = event.workflow.actionInvocation.inputs["value1"].integerValues[0];
  console.log("The user input is: ", successfulRun);

  // If successful, return an activity log linking to the created document.
  if (successfulRun == 1) {
    let logChip = AddOnsResponseService.newTextFormatChip()
      .setTextFormatIcon(
        AddOnsResponseService.newTextFormatIcon()
          .setMaterialIconName("edit_document")
      )
      .setUrl("https://docs.google.com/document/d/{DOCUMENT}")
      .setLabel("Mock Document");

    let output = AddOnsResponseService.newVariableData()
      .addStringValue("Created Google Doc");

    const workflowAction = AddOnsResponseService.newReturnOutputVariablesAction()
      .addVariableData("result", output)
      // Set the user-facing error log
      .setLog(
        AddOnsResponseService.newWorkflowTextFormat()
          .addTextFormatElement(
            AddOnsResponseService.newTextFormatElement()
              .setText("Created Google Doc")
          )
          .addTextFormatElement(
            AddOnsResponseService.newTextFormatElement()
              .setTextFormatChip(logChip)
          )
          .addTextFormatElement(
            AddOnsResponseService.newTextFormatElement()
              .setText("Created doc detailing how to improve product.")
          )
      );

    let hostAppAction = AddOnsResponseService.newHostAppAction()
      .setWorkflowAction(workflowAction);

    return AddOnsResponseService.newRenderActionBuilder()
      .setHostAppAction(hostAppAction)
      .build();
  }
  // Otherwise, return an activity log containing an error explaining what happened and how to resolve the issue.
  else {
    let errorChip = AddOnsResponseService.newTextFormatChip()
      .setTextFormatIcon(
        AddOnsResponseService.newTextFormatIcon()
          .setMaterialIconName("file_open")
      )
      .setLabel("Mock Document");

    const workflowAction = AddOnsResponseService.newReturnElementErrorAction()
      .setErrorActionability(AddOnsResponseService.ErrorActionability.ACTIONABLE)
      .setErrorRetryability(AddOnsResponseService.ErrorRetryability.NOT_RETRYABLE)
      // Set the user-facing error log
      .setErrorLog(
        AddOnsResponseService.newWorkflowTextFormat()
          .addTextFormatElement(
            AddOnsResponseService.newTextFormatElement()
              .setText("Failed to create Google Doc.")
          )
          .addTextFormatElement(
            AddOnsResponseService.newTextFormatElement()
              .setTextFormatChip(errorChip)
          )
          .addTextFormatElement(
            AddOnsResponseService.newTextFormatElement()
              .setText("Unable to create Google Document because OAuth verification failed. Grant one of these authorization scopes and try again: https://www.googleapis.com/auth/documents, \nhttps://www.googleapis.com/auth/drive, \nhttps://www.googleapis.com/auth/drive.file")
          )
      );

    let hostAppAction = AddOnsResponseService.newHostAppAction()
      .setWorkflowAction(workflowAction);

    return AddOnsResponseService.newRenderActionBuilder()
      .setHostAppAction(hostAppAction)
      .build();
  }
}

عرض سجلّات الأنشطة

لعرض سجلات الأنشطة، انتقِل إلى علامة التبويب "النشاط" في Workspace Studio. تعرض علامة التبويب هذه سجلّاً لعمليات تنفيذ الخطوات، بما في ذلك أي سجلّات مخصّصة أو رسائل خطأ حدّدتها.