Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

using excel with php redmine api to update custom field in alot of issues #435

Closed
ashrafalzyoud opened this issue Nov 13, 2024 · 1 comment
Labels

Comments

@ashrafalzyoud
Copy link

ashrafalzyoud commented Nov 13, 2024

can i use your plugin in this way
this chatgpt suggest
The php-redmine-api library itself doesn’t directly support reading Excel files, as its primary focus is on interacting with the Redmine API. However, you can easily add Excel file reading capability in PHP by combining it with a library like PhpSpreadsheet. This setup will allow you to read data from an Excel file, then use php-redmine-api to update Redmine issues.

Here’s a guide to reading data from Excel and updating issues in Redmine using PHP.

Step 1: Install Required Libraries
You’ll need to install both php-redmine-api and PhpSpreadsheet:

Install php-redmine-api:

bash
Copy code
composer require kbsali/redmine-api
Install PhpSpreadsheet:

bash
Copy code
composer require phpoffice/phpspreadsheet
Step 2: Prepare Your Excel File
Set up your Excel file with the following columns:

IssueID: Column A, containing the Redmine issue ID.
CustomField10: Column B, containing the new value for Custom Field 10 for each issue.
Save the file (e.g., UpdateIssues.xlsx).

Step 3: Write the PHP Script
Here’s a sample script that reads data from the Excel file and updates each issue’s custom field in Redmine.

php
Copy code

<?php

require 'vendor/autoload.php';

use Redmine\Client;
use PhpOffice\PhpSpreadsheet\IOFactory;

// Redmine server details
$redmineUrl = 'http://your-redmine-server.local';
$apiKey = 'your_api_key';
$client = new Client($redmineUrl, $apiKey);

// Path to your Excel file
$excelFilePath = 'UpdateIssues.xlsx';

// Load the Excel file
$spreadsheet = IOFactory::load($excelFilePath);
$worksheet = $spreadsheet->getActiveSheet();

// Loop through rows in Excel, starting from the second row to skip headers
foreach ($worksheet->getRowIterator(2) as $row) {
    $cellIterator = $row->getCellIterator();
    $cellIterator->setIterateOnlyExistingCells(false);

    $issueId = null;
    $customField10Value = null;

    foreach ($cellIterator as $cell) {
        if ($cell->getColumn() == 'A') {
            $issueId = $cell->getValue(); // Issue ID
        } elseif ($cell->getColumn() == 'B') {
            $customField10Value = $cell->getValue(); // Custom Field 10 value
        }
    }

    // Skip if there's no issue ID or custom field value
    if (empty($issueId) || empty($customField10Value)) {
        continue;
    }

    // Update the issue in Redmine
    $response = $client->api('issue')->update($issueId, [
        'custom_fields' => [
            [
                'id' => 10, // Custom Field ID for Custom Field 10
                'value' => $customField10Value
            ]
        ]
    ]);

    if ($response) {
        echo "Updated issue ID $issueId with value: $customField10Value\n";
    } else {
        echo "Failed to update issue ID $issueId\n";
    }
}

Explanation of the Script
Load Excel File: The script uses PhpSpreadsheet to load the Excel file and access each row.
Extract Issue Data: For each row, it reads IssueID and CustomField10.
Update Redmine Issue: Using php-redmine-api, the script updates the specified custom field for each issue ID.
Error Handling: If the update fails, it will output a failure message.
Running the Script
Update $redmineUrl, $apiKey, and $excelFilePath in the script to match your Redmine URL, API key, and Excel file location.
Run the script from the command line:
bash
Copy code
php your_script_name.php
This approach provides a flexible way to perform bulk updates in Redmine based on Excel data in PHP. Let me know if you need further customization!

@Art4
Copy link
Collaborator

Art4 commented Jan 21, 2025

I'm sorry, we cannot provide support for ChatGPT results.

@Art4 Art4 closed this as completed Jan 21, 2025
@Art4 Art4 closed this as not planned Won't fix, can't repro, duplicate, stale Jan 21, 2025
@Art4 Art4 added the question label Jan 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants