I have a Workbook that requires it to interact with another workbook. I have the below code which will run fine and get the value of the cell. But I need there to be a function to reference another Workbook.
function main(workbook: ExcelScript.Workbook) {
// Get Worksheet
let Sheet1 = workbook.getWorksheet('Worksheet 1');
// Get the value of cell A1.
let range = SourceData1.getRange("A1");
// Print the value of A1.
console.log(range.getValue());
}
I've tried referencing like so...
let SourceData1 = workbook.getWorksheet('https://mydomain.sharepoint.com/personal/Documents/[sourceData.xlsx]in');
But I take it the workbook part just references whatever is open? How do I refer an external workbook from a different sheet?
You should be able to use most of the response I wrote here. There are just a few small differences:
You do not need to stringify or parse anything. You can just work with the value of the cell as a string directly.
You would get the value of the cell in the first script using the range's GetValue method. You would then return that value as a string in the first script like so:
//first script
function main(workbook: ExcelScript.Workbook): string
{
let sh: ExcelScript.Worksheet = workbook.getActiveWorksheet();
let rang: ExcelScript.Range = sh.getRange("E1");
return rang.getValue() as string
}
In the second script, you wouldn't need to resize any ranges. Just set the range where the input value should be assigned, and assign the functions' parameter value to that (in my link I called the parameter tableValues. So since this is a cell value, I'd consider renaming it to something like cell value). So if you had a cellValue parameter you could just update the line of code like so:
//second script
function main(workbook: ExcelScript.Workbook, cellValue: string)
{
let sh: ExcelScript.Worksheet = workbook.getActiveWorksheet()
let rang: ExcelScript.Range = sh.getRange("D1")
rang.setValue(cellValue)
}