How to add a hyperlink to Excel cell using TMS Flexcel - tms

I am creating an Excel file using TMS Flexcel. I have a master worksheet that list all of the worksheets in the workbook. On the master worksheet I want to add in text with a link to all of the other worksheets.
I have the following code creating a hyperlink and adding it to the excel file.
var hyperLink = new THyperLink(THyperLinkType.CurrentWorkbook, inputFile.FileCode, "Link to another worksheet", inputFile.FileCode + "#A1", null);
So once I have added the hyperlink I can't figure out how to add it to a cell in the master worksheet. I tried the following but it won't work:
excelFile.SetCellValue(1, 2, hyperLink, 4);
What the above outputs in cell B2 is: "FlexCel.Core.THyperLink"

Here is the answer to my question. I forgot to go back and answer my question once I found a solution.
var link = new THyperLink(THyperLinkType.CurrentWorkbook, "", "Master List", "", "'Master List'!D" + (sheetIndex + 1));
excelFile.AddHyperLink(new TXlsCellRange(1, 1, 1, 1), link);
You have to use the AddHyperLink method to add a hyperlink to a cell.

Related

iText -Duplicate Fields Appear on last page

I am adding fields to an existing PDF using iText 7.NET. (7.1.16) When they are added, I wind up with a duplicate of every field on the last page. For example, in a two page document, with the below code, a second field with the same name, Field1 and red border will appear on page two, in the same position. New to iText7, guidance appreciated, Thank You
PdfReader reader = new PdfReader(inputFile);
PdfDocument pdf2 = new PdfDocument(reader, new PdfWriter(outputFile));
PdfAcroForm form = PdfAcroForm.GetAcroForm(pdf2, true);
PdfFormField tf = PdfTextFormField.CreateText(
pdf2, new Rectangle(1, 20, 30, 40), "Field1", "");
tf.SetPage(1);
tf.SetBorderColor(ColorConstants.RED);
tf.SetBorderWidth(2);
form.AddField(tf);
pdf2.Close();
So the help for the non-overloaded version of AddField(), without passing a PdfPage parameter mentions that it adds it to the last page of the document. I suppose the SetPage() on the field just results on it also being on the specified page.

Inserting text following selection in Office-JS

I am trying to append a hyperlink immediately following a piece of selected text in Word 2013 using the Shared JS API.
Inserting the hyperlink using OOXML works fine when inserting at current cursor with no selection. My problem is 'finding' the end of the selected text to append the OOXML.
Just using setSelectedDataAsync overwrites the existing text. I have tried reading the selected text as OOXML and concatenating the hyperlink XML to it but without success.
I have not tried reading the current selection and then modifying that OOXML but would prefer to avoid.
In the Word JS API a before and after are provided on the selection so it is straightforward to do. Is it possible to do this in the Shared API? Thanks.
The following code sample illustrates the approach that Marc described in his comment above (with one exception: it gets the selected data as Text, not as HTML).
This snippet uses getSelectedDataAsync to get the selected data (as Text), and then appends a hyperlink to that data and uses setSelectedDataAsync to push that string back into the document (as HTML).
Office.context.document.getSelectedDataAsync(Office.CoercionType.Text,
{ valueFormat: "unformatted" },
function (asyncResult) {
var error = asyncResult.error;
if (asyncResult.status === Office.AsyncResultStatus.Failed) {
console.log(error.name + ": " + error.message);
}
else {
// Get selected data.
var dataValue = asyncResult.value;
console.log("Selected data is: " + dataValue);
// Create newText by appending hyperlink to dataValue.
var myHyperlink = "<a href='https://www.bing.com'>https://www.bing.com</a>";
var newText = dataValue + " " + myHyperlink;
console.log("New text is: " + newText);
// Replace selected text with newText value.
Office.context.document.setSelectedDataAsync(newText, { coercionType: "html" },
function (asyncResult) {
var error = asyncResult.error;
if (asyncResult.status === Office.AsyncResultStatus.Failed) {
console.log(error.name + ": " + error.message);
}
});
}
});
Note: One side-effect of getting the selected data as Text, as this snippet does, is that when you write back that string (with the hyperlink appended) to the document, you'll lose any formatting (for example: font color, style, etc.) that was previously present in the selected text. If it's important that you preserve formatting of the originally selected text, you'll need to get the selected data as HTML and then append your hyperlink to the the portion of HTML which contains the originally selected text, before writing that HTML back to the document.
You can quickly and easily try this code snippet yourself in Word by using Script Lab (https://aka.ms/getscriptlab). Simply install the Script Lab add-in (free), then choose "Import" in the navigation menu, and use the following GIST URL: https://gist.github.com/kbrandl/8e235fb0ccc190bf42ed9ce1874f5559.

Getting values from cells in google scripts

I am trying to make working sheets for my work. In Google scripts, I've created "Custom Menu" for my sheet wich is sending email correctly. But now I want to get value from the specific cell and check if it is below, for example, 2, send an email with that value. For now, I have this:
function onOpen() {
var ui = SpreadsheetApp.getUi();
// Or DocumentApp or FormApp.
ui.createMenu('Custom Menu')
.addItem('First item', 'menuItem1')
.addSeparator()
.addToUi();
}
function menuItem1() {
SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
.alert('You clicked the first menu item!');
if( 'A1' > 3){
MailApp.sendEmail('luk...#gmail.pl', 'subject', 'message');
}
}
I don't know how to get this value from this cell. This 'If" is just an example of what I am trying to do, I know it is not working. Thank you in advance for any kind of help.
First, You need to find the sheet:
var sheet = SpreadsheetApp.getActiveSheet();
Then, you need to specify a cell range and get the value(s):
var value = sheet.getRange("A1").getValue();
You can browse the API for more functions here: https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet-app

Google Sheets - Code to send an email to an address in a cell upon selection of a dropdown menu cell

I know very little about code and am simply looking for help if someone knows how to do this. I have a google spreadsheet and for this example let's say
Column A is an email address
Column B is text
Column C is a dropdown list of items made using the Data Validation tool (pending, scheduled and complete).
I need a code that upon selecting "Complete" in column C, an email is sent to the recipient in column A with the body of the email containing the text from column B.
Any help is appreciated as I'm not a developer, I'm a pastor trying to help my church run more smooth.
and here's the link to the spreadsheet... https://docs.google.com/spreadsheets/d/1bA-gDvZ_jbJMyU6IqExBjHKclrKmqJSMHyuKHsr2CMA/edit?usp=sharing
and here's the script...
var sheetname = "FacilitiesWorkRequests";
function onOpen() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetname);
var range = sheet.getRange(1, 3, sheet.getLastRow(), 1);
var list = SpreadsheetApp.newDataValidation().requireValueInList(["pending", "scheduled", "complete"], true).build();
range.setDataValidation(list);
}
function onEdit() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetname);
var ac = sheet.getActiveCell();
if (ac.getValue() == "complete") {
data = sheet.getRange(ac.getRowIndex(), 1, 1, 2).getValues();
Logger.log(data)
MailApp.sendEmail({
to: data[0][0],
subject: "sample mail",
body: data[0][1]
});
}
}
How about this sample?
var sheetname = "Here, please input your sheet name.";
function onOpen() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetname);
var range = sheet.getRange(1, 3, sheet.getLastRow(), 1);
var list = SpreadsheetApp.newDataValidation().requireValueInList(["pending", "scheduled", "complete"], true).build();
range.setDataValidation(list);
}
function onEdit() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetname);
var ac = sheet.getActiveCell();
if (ac.getValue() == "complete") {
data = sheet.getRange(ac.getRowIndex(), 1, 1, 2).getValues();
Logger.log(data)
MailApp.sendEmail({
to: data[0][0],
subject: "sample mail",
body: data[0][1]
});
}
}
Please copy and paste this script to the script editor on spreadsheet, and set a sheet name you use, and then install a trigger. How to install a trigger is as follows. You can see the detailed information of this at https://developers.google.com/apps-script/guides/triggers/installable#google_apps_triggers.
At the script editor, choose "Resources" - "Current project's triggers".
Click "here" and add a trigger.
Under Run, select the function of "onEdit()".
Event is "From spreadsheet" - "Change of value".
For this sample script, when you open spreadsheet with this script, the dropdown list is applied, while the existing dropdown list is not changed. Because if you had added new row, new dropdown list is added. When you run this script, if authentication screen appears, please authenticate it.
And when you choose "complete" from the dropdown list, an e-mail is sent using e-mail of same row you chose. E-mail body is column B of same row.
If this will be helpful, I'm glad.

Script for removing rows in Google sheets

I'm in need of a script that removes, not just deleting, rows in Google sheets.
Having a form that supplies responses to a sheet, and for now I have to manually log in and remove the responses.
So a simple script that triggers every saturday, removing form responses from a sheet is what I need.
Code to delete rows:
function deleteResponses() {
var ss = SpreadsheetApp.openById("File ID");
var sheet = ss.getSheets()[3];
sheet.deleteRows(2, 200);
};
To add code: Under the TOOLS menu, choose SCRIPT EDITOR. Add this code.
Alternate code to clear the sheet:
function deleteResponses() {
var ss = SpreadsheetApp.openById(id);
var sheet = ss.getSheets()[0];
sheet.clear();
};
Get your spreadsheet ID, and replace it in the "id" parameter. Then under the RESOURCES menu, choose CURRENT PROJECTS TRIGGERS.