humanemulator.net — справка. Продукт, демо и покупка — на хуманэмулятор.рф | Демо | Купить | API | Changelog

get_and_clear_range

get_and_clear_range($path,$sheet,$begin_cell,$end_cell,$timeout=3000); - Получить значения заданного диапазона Листа как массив строк и очистить ячейки диапазона

Функция на вход принимает параметры:

  • $path – путь к исходному файлу Excel
  • $sheet – Номер Листа
  • $begin_cell – адрес ячейки начала диапазона. Пример: "A1"
  • $end_cell – адрес ячейки конца диапазона. Пример: "A10"
  • $timeout – Таймаут ожидания выполнения операции, сек

    После выполнения функции возвращаемое значение будет равно одному из двух :
  • результат – array|null
  • пусто / null – ошибка

    Пример использования
    <?php 
    // Scenario: Demonstrates how to get the content of a range as an array and then clear that range
    
    $xhe_host = getenv("RPABOT_HOST_URL");
    // Connect functional objects if not already connected
    if (!isset($path)){
        // Path to the init.php file for connecting to the XHE API
        $path = "../../../Templates/init.php";
        // Including init.php grants access to all classes and functionality for working with the XHE API
        require($path);
    }
    
    // Set variables for method arguments
    $filePath = "test/test.xlsx";
    $sheetIndex = 1;
    $rowIndex = 1;
    $rangeStart = "A1";
    $rangeEnd = "H1";
    $rowData = array('Завод "Э"', 2, 3, 4, 5, 6, 7, 8);
    
    // Kill any existing Excel processes
    SYSTEM::$excel->kill();
    
    // Example 1: Open Excel file
    echo("\nExample 1: Open Excel file\n");
    $result = SYSTEM::$excel->open($filePath, false, true);
    if ($result) {
        echo("Successfully opened file $filePath\n");
    } else {
        echo("Failed to open file $filePath\n");
    }
    
    // Example 2: Set row values
    echo("\nExample 2: Set row values\n");
    $result = SYSTEM::$excel->set_row($filePath, $sheetIndex, $rowIndex, $rowData);
    if ($result) {
        echo("Successfully set values for row #$rowIndex\n");
    } else {
        echo("Failed to set values for row #$rowIndex\n");
    }
    
    // Example 3: Get range content as array and clear it
    echo("\nExample 3: Get range content as array and clear it\n");
    $rangeData = SYSTEM::$excel->get_and_clear_range($filePath, $sheetIndex, $rangeStart, $rangeEnd);
    if ($rangeData) {
        echo("Successfully got content of range $rangeStart:$rangeEnd and cleared it:\n");
        var_export($rangeData);
    } else {
        echo("Failed to get and clear range $rangeStart:$rangeEnd\n");
    }
    
    // Example 4: Save the file
    echo("\nExample 4: Save the file\n");
    $result = SYSTEM::$excel->save($filePath);
    if ($result) {
        echo("Successfully saved file $filePath\n");
    } else {
        echo("Failed to save file $filePath\n");
    }
    
    // Wait for 3 seconds to observe the result
    sleep(3);
    
    // Example 5: Get range content (should be empty now)
    echo("\nExample 5: Get range content (should be empty now)\n");
    $rangeData = SYSTEM::$excel->get_range($filePath, $sheetIndex, $rangeStart, $rangeEnd);
    if ($rangeData) {
        echo("Content of range $rangeStart:$rangeEnd after clearing:\n");
        var_export($rangeData);
    } else {
        echo("Range $rangeStart:$rangeEnd is empty or could not be retrieved\n");
    }
    
    // Example 6: Close the file
    echo("\nExample 6: Close the file\n");
    $result = SYSTEM::$excel->close($filePath);
    if ($result) {
        echo("Successfully closed file $filePath\n");
    } else {
        echo("Failed to close file $filePath\n");
    }
    
    // Quit the application
    WINDOW::$app->quit();
    ?>
    # Additional paths
    import sys
    sys.path.insert(0, '../../../Templates PY/')
    
    xhe_host = "127.0.0.1:7010"
    from xweb_human_emulator import *
    
    # начало
    echo("<hr><font color=blue>excelfile.get_and_clear_range</font><hr>")
    
    path = "test/test.xlsx"
    
    excel.kill()
    
    echo("\n1. Создать значения для строки: ")
    echo(excelfile.set_row(path, 1, 1,  ['Завод "Э"', 2, 3, 4, 5, 6, 7, 8]))
    
    echo("\n2. Вырезать и вставить диапазоны: ")
    echo(excelfile.get_and_clear_range(path, 1, "A1", "H1"))
    
    # Для наблюдения за результатом работы
    app.pause(3)
    
    echo("\n3. Получить содержимое диапазона (пусто): ")
    echo(excelfile.get_range(path, 1, "A1", "H1"))
    
    
    # конец
    echo("<hr><br>")
    
    # Quit
    app.quit()
    #region using
    
    using System;
    using System.Diagnostics;
    using System.Collections.Generic;
    using System.Linq;
    using System.IO;
    using System.Text;
    using System.Threading;
    
    using XHE;
    using XHE.XHE_DOM;
    using XHE.XHE_System;
    using XHE.XHE_Window;
    using XHE.XHE_Web;
    
    #endregion
    
     class Program:XHEScript
     {
    	  static void Main(string[] args)
    	  {
    			// init XHE
    			server = Environment.GetEnvironmentVariable("RPABOT_HOST_URL");
    			InitXHE();
    
    			var path = "test/test.xlsx";
    
    			// начало
    			echo("<hr><font color=blue>excel.get_and_clear_range</font><hr>");
    
                // Закрыть все процессы Excel
    			excel.kill();
    
                echo("1. Откроем: ");
                echo(excel.open(path, false, true));
    
                echo("\n2. Создать значения для строки: ");
                echo(excel.set_row(path, 1, 1, array('Завод "Э"', 2, 3, 4, 5, 6, 7, 8)));
    
    			echo("\n3. Получить содержимое диапазона как массив и очистить: ");
                print_r(excel.get_and_clear_range(path, 1, "A1", "H1"));
    
                echo("\n4. Сохранить: ");
                excel.save(path);
                // Для наблюдения за результатом работы
                sleep(3);
    
                echo("\n5. Получить содержимое диапазона (пусто): ");
                print_r(excel.get_range(path, 1, "A1", "H1"));
    
                echo("\n6. Закрыть: ");
                excel.close(path);
    
    			// конец
    			echo("\n\n");
    
    			app.quit();            
    	  }
    }
    xhe_host="127.0.0.1:7010";
    echo=require("../../../Templates JS/init.js");
    
    // начало
    echo("<hr><font color=blue>excel.get_range</font><hr>");
    
    const path = "test/test.xlsx";
    
    // Закрыть все процессы Excel
    excel.kill();
    
    echo("1. Откроем: ");
    echo(excel.open(path, false, true));
    
    echo("\n2. Создать значения для строки: ");
    echo(excel.set_row(path, 1, 1, ['Завод "Э"', 2, 3, 4, 5, 6, 7, 8]));
    
    echo("\n3. Получить содержимое диапазона как массив и очистить: ");
    let res = excel.get_and_clear_range(path, 1, "A1", "H1");
    console. log(res);
     
    echo("\n4. Сохранить: ");
    excel.save(path);
    // Для наблюдения за результатом работы
    app.sleep(3);
    
    echo("\n5. Получить содержимое диапазона (пусто): ");
    res = excel.get_range(path, 1, "A1", "H1");
    console. log(res);
    
    // конец
    echo("\n");
    
    // Quit
    app.quit();

    =============================================
    excel    Объекты    DOM  System  Vision  Web  Window        
    =============================================
    если что-то непонятно или необходимо узнать или считаете что надо добавить по работе этой функции, пишите в комментарии или на наш форум
    .