Miramac / node-office-script

Scripting MS Office application with node.js
ISC License
49 stars 5 forks source link

Wondering how can i add a new function like "slide hyperlink" #17

Open popsugon opened 6 years ago

Miramac commented 6 years ago

A good question, because unfortunately there is no documentation yet on how to contribute to the project.

Add the function in C

In your case you want to add this function as a shape function.

/src/OfficeScript/OfficeScript/Report/Shape.cs

/// <summary>
/// Add a hyperlink to the shape
/// </summary>
/// <param name="parameters"></param>
private void AddHyperlink(Dictionary<string, object> parameters)
{
    // Some code to add a Hyperlink to a schape
}

Make the function public to Javascript

Add a new async function to the return object of Invoke() /src/OfficeScript/OfficeScript/Report/Shape.cs

public object Invoke()
{
    return new
    {
        attr = (Func<object, Task<object>>)(
            async (input) =>
            {
                if (input is string)
                {
                    var tmp = new Dictionary<string, object>();
                    tmp.Add("name", input);
                    input = tmp;
                }
                return Util.Attr(this, (input as IDictionary<string, object>).ToDictionary(d => d.Key, d => d.Value), Invoke);
            }),

        // . . . other code . . .

        // your new hyperlink function
        addHyperlink = (Func<object, Task<object>>)(
            async (input) =>
            {
                this.AddHyperlink((input as IDictionary<string, object>).ToDictionary(d => d.Key, d => d.Value));
                return this.Invoke(); // return the shape.Invoke() result for chaining
            })
    };
}

Compile the C# code

gulp build

Create a javascript wrapper

/lib/powerpoint/shape.js

shape.addHyperlink = function (address, subAddress) {
  return nativeShape.addHyperlink({
    address: address,
    subAddress: subAddress
  }, true)
}

Make a pull requests

I would love to add your new function to the library!

popsugon commented 6 years ago

Hi @Miramac ,

it would be greater if you add the function about "shape.Table add rows/delete rows".

Miramac commented 6 years ago

Yes, the function for adding/deleting rows/columns in tables is missing. Unfortunately not a fast finished task, how the tables are currently being processed.