How to add hyperlink to Excel simple shape using apache poi? - excel-2007

I use Microsoft Excel 2007. I have a simple shape on the first excel sheet. I would like to add a hyperlink on this simple shape which refer to another sheet on the specific row and column. I make a research about this but I only found examples which demonstrate how to add a hyperlink to a given cell. How can I add a hyperlink to a given simple shape with the help of Apache POI?

I found solution of my issue. I hope it will help to someone else.
// Example for hyperlink address
// String hyperlinkAddress = sheet.getPackagePart().getPartName() + "/#'Sheet name'!Cell Address"
// My hyperlink address
String hyperlinkAddress = sheet.getPackagePart().getPartName()
+ "/#'List_of_Items'!A12";
// Create URI object which will containing our hyperlink address.
URI uri = new URI(null, null, hyperlinkAddress, null, null);
// Add relationship to XSSFDrawing object.
aPatriarch.getPackagePart().addRelationship(uri, TargetMode.INTERNAL,
XSSFRelation.SHEET_HYPERLINKS.getRelation());
// We need to extract the ID of the Relationship.
// We'll set the ID of the hyperlink to be the same as ID of the Relationship.
// To find appropriate Relationship we will traverse through all Relationships in the 'aPatriarch' object.
String relationshipId = null;
PackageRelationshipCollection prc = aPatriarch.getPackagePart().getRelationships();
for (PackageRelationship pr : prc) {
URI targetURI = pr.getTargetURI();
String target = targetURI.getPath();
if (target.equals(hyperlinkAddress)) {
relationshipId = pr.getId();
break;
}
}
// Create the hyperlink object
CTHyperlink hyperlink = CTHyperlink.Factory.newInstance();
// Set ID of hyperlink to be the same as Relationship ID
hyperlink.setId(relationshipId);
// Add hyperlink to the given shape
shape.getCTShape().getNvSpPr().getCNvPr().setHlinkClick(hyperlink);

Related

Append to meeting invite body with or without Redemption

We are developing an Outlook VSTO add-in.
Right now I am trying to append some information to a meeting invite the user is in the process of composing. I want the content to appear in the body like what clicking the Teams-meeting button would do, where formatted text and links are appended to the end of the body.
Since the content is HTML and the Outlook Object Model does not expose an HTMLBody property for AppointmentItems, I try to set it via Redemption:
// Dispose logic left out for clarity but everything except outlookApplication and outlookAppointment is disposed after use
Application outlookApplication = ...;
AppointmentItem outlookAppointment = ...; // taken from the open inspector
NameSpace outlookSession = outlookApplication.Session;
RDOSession redemptionSession = RedemptionLoader.new_RDOSession();
redemptionSession.MAPIOBJECT = outlookSession.MAPIOBJECT;
var rdoAppointment = (RDOAppointmentItem)redemptionSession.GetRDOObjectFromOutlookObject(outlookAppointment);
string newBody = transform(rdoAppointment.HTMLBody); // appends content right before HTML </body> tag
rdoAppointment.BodyFormat = (int)OlBodyFormat.olFormatHTML;
rdoAppointment.HTMLBody = newBody;
Problem
The Outlook inspector window is not updating with the appended content. If I try to run the code again, I can see the appended content in the debugger, but not in Outlook.
Things I have tried:
Saving the RDOAppointmentItem
Also adding the content to Body property
Using SafeAppointmentItem instead of RDOAppointmentItem; didn't work because HTMLBody is a read-only property there
Setting PR_HTML via RDOAppointment.Fields
Paste the HTML via WordEditor (see below)
Attempt to use WordEditor
Per suggestion I also attempted to insert the HTML via WordEditor:
// Dispose logic left out for clarity but everything except inspector is disposed after use
string htmlSnippet = ...;
Clipboard.SetText(htmlSnippet, TextDataFormat.Html);
Inspector inspector = ...;
Document wordDoc = inspector.WordEditor;
Range range = wordDoc.Content;
range.Collapse(WdCollapseDirection.wdCollapseEnd);
object placement = WdOLEPlacement.wdInLine;
object dataType = WdPasteDataType.wdPasteHTML;
range.PasteSpecial(Placement: ref placement, DataType: ref dataType);
... but I simply receive the error System.Runtime.InteropServices.COMException (0x800A1066): Kommandoen lykkedes ikke. (= "Command failed").
Instead of PasteSpecial I also tried using PasteAndFormat:
range.PasteAndFormat(WdRecoveryType.wdFormatOriginalFormatting);
... but that also gave System.Runtime.InteropServices.COMException (0x800A1066): Kommandoen lykkedes ikke..
What am I doing wrong here?
EDIT: If I use Clipboard.SetText(htmlSnippet, TextDataFormat.Text); and then use plain range.Paste();, the HTML is inserted at the end of the document as intended (but with the HTML elements inserted literally, so not useful). So the general approach seems to be okay, I just can't seem to get Outlook / Word to translate the HTML.
Version info
Outlook 365 MSO 32-bit
Redemption 5.26
Since the appointment is being displayed, work with the Word Object Model - Inspector.WordEditor returns the Document Word object.
Per Dmitrys suggestion, here is a working solution that:
Shows the inserted content in the inspector window.
Handles HTML content correctly with regards to both links and formatting (as long as you stay within the limited capabilities of Words HTML engine).
using System;
using System.IO;
using System.Text;
using Outlook = Microsoft.Office.Interop.Outlook;
using Word = Microsoft.Office.Interop.Word;
namespace VSTO.AppendHtmlExample
{
public class MyExample
{
public void AppendAsHTMLViaFile(string content)
{
// TODO: Remember to release COM objects range and wordDoc and delete output file in a finally clause
Outlook.Inspector inspector = ...;
string outputFolderPath = ...;
string outputFilePath = Path.Combine(outputFolderPath, "append.html");
Word.Document wordDoc = inspector.WordEditor;
File.WriteAllText(outputFilePath, $"<html><head><meta charset='utf-8'/></head><body>{content}</body></html>", Encoding.UTF8);
Word.Range range = wordDoc.Content;
range.Collapse(Word.WdCollapseDirection.wdCollapseEnd);
object confirmConversions = false;
object link = false;
object attachment = false;
range.InsertFile(fileName,
ConfirmConversions: ref confirmConversions,
Link: ref link,
Attachment: ref attachment);
}
}
}

How to save as image a QR codes created in Google Sheets with api.qrserver?

I am generating QR codes in a column in Google Sheets with data entered via a Google Forms, and I would like to save them as images.
Using Ctrl + C and Ctrl+V works only inside the Google Sheet and trying to copy and paste that one outside the sheet doesnt work either. The clipboard is detected as not containing images to paste elsewhere.
The problem is not in generating the QR, but exporting or saving it as image.
To generate the QR's I'm using the api.qrserver, but the documentation for this only goes about creating or reading QR's [http://goqr.me/api/doc/] , for which this base is used to technically generate a PNG image:
http(s)://api.qrserver.com/v1/create-qr-code/?data=[URL-encoded-text]&size=[pixels]x[pixels]
However, If I use that formula externally to link directly to the cell, the PNG created only gives the URL address of the Google Sheet and not the information of the QR.
This is the array formula I'm using at row 1 to generate the QR's:
={"QR";arrayformula( if(
len(INDIRECT("A2:A")),IMAGE("https://api.qrserver.com/v1/create-qr-
code/?size=120x120&data="&ENCODEURL(
"-Tip: "&indirect("B2:B")& char(10)&
"-Ub: "&INDIRECT("C2:C")& char(10)&
"-#: "&indirect("D2:D")& char(10)&
"-IDE: "&INDIRECT("E2:E")& char(10)&
"-Serial: "&INDIRECT("G2:G") & char(10)&
"-Pa: "&indirect("H2:H") & char(10)&
"-IP: "&INDIRECT("I2:I") & char(10)&
"-As: "&indirect("J2:J") & char(10)&
"-R: "&INDIRECT("K2:K") & char(10)&
"-Ar: "&indirect("L2:L") & char(10)
),2)
,""))}
Ideally, I would like to automatically make the QR's as images and save them to a folder in google drive automatically. If possible, assigning them an IDE value that is contained in another column as well as in the same QR code.
Here is the example for the QR generation:
[https://docs.google.com/spreadsheets/d/16pALQ0LNbBqqdRNNejar8zJJPPNEP8I7HSbKDOJHZaw/edit?usp=sharing ]
You want to save the QR codes in the Spreadsheet as the image files using Google Apps Script.
In this case, you want to achieve this without changing the Spreadsheet.
Values are put to from the column "A" to "L", when the form was submitted.
If my understanding is correct, how about this sample script? Please think of this as just one of several answers.
In this sample script, the QR code is directly retrieved from api.qrserver.com and those are saved as the image files, because in your situation, the images cannot be retrieved from the Spreadsheet. In this case, all parameters of QR code are used from the Spreadsheet.
Sample script:
Before you run the script, please set the variables of sheetName and folderId. When you run this script, all QR codes in the sheet of Hoja_calculos are created as the image files.
function myFunction() {
var sheetName = "Hoja_calculos"; // Sheet name is from your shared Spreadsheet.
var folderId = "###"; // Folder ID of the folder where the image files are created.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName(sheetName);
var url = "https://api.qrserver.com/v1/create-qr-code/?size=120x120&data=";
var keys = ["-Tipo:", "-UbicaciĆ³n:", "-# registro:", "-ID-C:", "-# Serial:", "-Pas:", "-Di I:", "-Asi:", "-RP:", "-Ar:"];
var values = sheet.getRange(2, 2, sheet.getLastRow() - 1, 11).getValues()
.filter(function(e) {return e.some(function(f) {return f})})
.map(function(row) {
row.splice(4, 1);
return row;
});
var length = values.length;
// values = [values.pop()]; // If you want to save only the QR code of the last row, please use this line.
var reqs = values.map(function(e) {return {url: url + encodeURIComponent(keys.map(function(f, j) {return f + e[j]}).join("\r"))}});
var res = UrlFetchApp.fetchAll(reqs);
res.forEach(function(r, i) {
var blob = r.getBlob().setName(length == values.length ? "row" + (i + 2) + ".png" : "row" + (length + 1) + ".png");
DriveApp.getFolderById(folderId).createFile(blob);
});
}
Note:
If you want to save only the QR code of the last row, please use this line. At that time, please remove // of // values = [values.pop()];. By this, only the QR code of the last row is retrieved.
Because I thought that above situation might be required for your situation, I added it. For example, by using the installable trigger to the function, when the form was submitted, only QR code of the last row is created as the image file.
But I'm not sure about your environment of the form. So I'm not sure whether this can be used for your situation. I apologize for this.
I confirmed that this script worked for your shared Spreadsheet. If the actual Spreadsheet is different from this, please modify the script, because it might not work fine.
References:
Class UrlFetchApp
Class DriveApp
You can not use a custom function and interacts with Google Drive, there is a limitation to access user data using custom function and onEdit function on G script:
https://developers.google.com/apps-script/guides/sheets/functions
AFAIK the only way to achieve this is creating a custom menu and interacts with a custom function by menu.
var url = "https://api.qrserver.com/v1/create-qr-code/?data=Sample&size=128x128";
var folders = DriveApp.getFoldersByName("QRCodeFolderName");// Put here the name of the folder inside root folder
var folder = folders.next();
var response = UrlFetchApp.fetch(url);
if (response.getResponseCode() == 200){
var fileBlob = response.getBlob();
folder.createFile(qrcodevalue+".png",fileBlob);
}

Indesign script: how to pasteboard items

I have this script that finds a paragraph style, puts an item from a library at the very end and applies object style:
myDoc = app.documents[0];
myLib = app.libraries[0];
myObjectStyle = myDoc.objectStyles.item ("marker");
app.findTextPreferences = app.changeTextPreferences = null;
app.findTextPreferences.appliedParagraphStyle = "Custom"
var myFound = app.activeDocument.findText(true);
alert (myFound.length);
try {
for (i = 0; i < myFound.length; i++) {
myIcon = myLib.assets.itemByName("winieta_tr").placeAsset (myFound[i].insertionPoints[-2])[0];
myIcon.appliedObjectStyle = myObjectStyle;
// myFound[i].remove ();
}
}
catch (e) {alert (e.message)}
I don't know how to alter it, so the items are obtained not from library but form pasteboard - any help would be appreciated.
Is it possible to find elements that are in the document by name, as it is with library elements?
Yes, you can find an object by name (you would assign that name in the layers panel) simply by using
myDoc.pageItems.itemByName("myItemName");
If you are looking for the same thing on a specific spread (for example if several items on several spreads have the same name), you can use
myDoc.spreads[0].pageItems.itemByName("myItemName");
Or if you just want to use the currently active spread
app.activeWindow.activeSpread.pageItems.itemByName("myItemName");
Just make sure not to use the page to address a page item on the pasteboard as the pasteboard does not belong to any page.
Is it possible to find elements that are in the document by name, as it is with library elements?
You can apply a script labels to the frame on the pasteboard to give it a name.

Check Word Doc for Field Code before applying

So I've coded a VSTO addin using vb.net to add a header to a document in Word however from historical methods we have lots of templates with field codes. My addin does not account for these and simply strips the header to add xxxxx value you choose from the pop up.
I need my code to be smart enough to 'spot' the field code and append or if it does not exist e.g. a blank document then continue running as expected. I can append this field code using the below code:
wordDocument.Variables("fieldname").Value = "xxxx"
wordDocument.Fields.Update
However my tool then adds the header as normal and strips most the content from the template. So effectively my question is how would I code a check for this before proceeding. So in plain English I would need my addin to go from this:
Load pop up
Set xxxx value in header
Close
To this:
Load pop up
Check Document for existing "fieldname"
If "fieldname" exists then
wordDocument.Variables("fieldname").Value = "xxxx" (from pop up selection)
wordDocument.Fields.Update
However if "fieldname" doesn't exist then continue as normal....
Sorry if this is a little complex and/or long winded.
Thanks in advance.
Here is my code in C#, hope this might help you to code in VB.Net
foreach (Section sec in doc.Sections)
{
doc.ActiveWindow.View.set_SeekView(WdSeekView.wdSeekCurrentPageHeader);
foreach (HeaderFooter headerFooter in sec.GetHeadersFooters())
{
doc.ActiveWindow.View.set_SeekView(headerFooter.IsHeader ? WdSeekView.wdSeekCurrentPageHeader : WdSeekView.wdSeekCurrentPageFooter);
if (headerFooter.Range.Fields.Count > 0)
{
//Append to existing fields
UpdateFields(headerFooter.Range.Fields);
}
else
{
//Add field code
AddFieldCode(headerFooter.Range);
}
}
doc.ActiveWindow.View.set_SeekView(WdSeekView.wdSeekMainDocument);
}
Extension method to iterate through the header types
public static IEnumerable<HeaderFooter> GetHeadersFooters(this Section section)
{
List<HeaderFooter> headerFooterlist = new List<HeaderFooter>
{
section.Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary],
section.Headers[WdHeaderFooterIndex.wdHeaderFooterFirstPage],
section.Headers[WdHeaderFooterIndex.wdHeaderFooterEvenPages],
section.Footers[WdHeaderFooterIndex.wdHeaderFooterPrimary],
section.Footers[WdHeaderFooterIndex.wdHeaderFooterFirstPage],
section.Footers[WdHeaderFooterIndex.wdHeaderFooterEvenPages]
};
return headerFooterlist;
}

Infopath 2010 > is multiple selection list box allowed for Browser

I may be missing something. I have a Multiple Selection List box in a Section. See attached pic. When I right click on the grpApplications and copy XPath it gives me /my:myFields/my:grpAccessOfficeEquipment/my:grpApplications when I right click for XPath on the fldApplicatins it gives me /my:myFields/my:grpAccessOfficeEquipment/my:grpApplications/my:fldApplications
In the code (code posted below) I am referencing the /my:myFields/my:grpAccessOfficeEquipment/my:grpApplications/my:fldApplications and I get no value. but I used /my:myFields/my:grpAccessOfficeEquipment/my:grpApplications I get a value for selected item in the listbox.
But I am still getting error NullReferenceException "object reference not set to an instance of an object" on string appName line. The AppID looks like this "\n\t\t\t262265264143\n\t\t" instead of whole number in debug. but when I click on the binocular is shows 262265264143 but with spaces/lines before and after this
XPathNavigator MainDS = MainDataSource.CreateNavigator();
XPathNodeIterator iterApp = MainDS.Select("/my:myFields/my:grpAccessOfficeEquipment/my:grpApplications", NamespaceManager);
System.Text.StringBuilder sb = new System.Text.StringBuilder();
while (iterApp.MoveNext())
{
string AppID = iterApp.Current.Value;
//int lookupID = Convert.ToInt16(value);
XPathNavigator secAPPDS = DataSources["SupportEscalationList"].CreateNavigator();
string appName = secAPPDS.SelectSingleNode("/dfs:myFields/dfs:dataFields/d:SharePointListItem_RW[#ID = '" + AppID + "']/#ApplicationProcess", NamespaceManager).Value;
sb.Append(appName);
item["ApplicationProcess"] = sb.ToString(); // because applicationprocess column in target sp list is single text type column
}
Says here multiple selection list boxes are supported in 2010 browser forms but not 2007 browsers forms. Hope that helps.
http://office.microsoft.com/en-us/infopath-help/infopath-2010-features-unavailable-in-web-browser-forms-HA101732796.aspx