简体   繁体   English

Officescript如何通过行索引在表格单元格(行/列)中设置值

[英]Officescript how to set value in table cell (row/column) by row index

pretty simple question but I can't seem to find what I am looking for and wondering if it is possible this way, just starting using Officescript/typescript.非常简单的问题,但我似乎无法找到我正在寻找的东西,并且想知道是否有可能,只是开始使用 Officescript/typescript。 In a part of my code, I get the index of the row with a value that matches (cRow is the index of row I am interested in).在我的代码的一部分中,我获得了具有匹配值的行的索引(cRow 是我感兴趣的行的索引)。

  rowValue = collectionTable.getColumnByName("SomeCol").getRangeBetweenHeaderAndTotal().getValues()[cRow]

And then I run some checks on that row and want to update some other things based on the inputs.然后我对该行进行一些检查,并希望根据输入更新一些其他内容。

So what I am expecting to do is something like the following, changing getValues for setValues:所以我期望做的是如下的事情,改变 setValues 的 getValues:

collectionTable.getColumnByName("UpdateMe").getRangeBetweenHeaderAndTotal().setValues()[cRow]

OR或者

let col = collectionTable.getColumnByName("SomeCol").getIndex();
let cell = collectionTable.getCell(requestRow,col);

cell.setValue(value);

But doesn't seem to work that way.. From what I can tell, setValues works on ranges but can't quite find how to get the range/row by index number and set a value in one cell.但似乎不是那样工作。据我所知,setValues 适用于范围,但无法完全找到如何通过索引号获取范围/行并在一个单元格中设置值。 I see all examples doing it with the letter and number but don't want to do it that way if possible.我看到所有的例子都是用字母和数字来做的,但如果可能的话,我不想那样做。

Thanks for the help!谢谢您的帮助!

Can get the value by using the worksheet cell, but I would really like to reference the table itself to not have any issues if things are moved around and such..可以通过使用工作表单元格来获取值,但我真的很想引用表格本身,以便在移动东西等情况下没有任何问题。

let col = collectionTable.getColumnByName("SomeCol").getIndex(); 

// if table starts at beginning of sheet, or else have to offset col and row..
let cell = dataSheet.getCell(requestRow + 1, col);

cell.setValue(value);

You can use getCell with the cRow variable after getRangeBetweenHeaderAndTotal() to get a specific cell range.您可以在 getRangeBetweenHeaderAndTotal() 之后使用带有 cRow 变量的 getCell 来获取特定的单元格范围。 Once you have that range, you can read its value using getValue().一旦你有了那个范围,你就可以使用 getValue() 来读取它的值。 And you can write a value to that range using setValue().您可以使用 setValue() 将值写入该范围。 You can see an example of how to do that below:您可以在下面看到如何执行此操作的示例:

function main(workbook: ExcelScript.Workbook)
{
  let cRow: number = 1
  let tbl: ExcelScript.Table = workbook.getTable("table1")
  let rowCell: ExcelScript.Range = tbl.getColumnByName("SomeCol").getRangeBetweenHeaderAndTotal().getCell(cRow,0)
  let rowValue: string = rowCell.getValue() as string
  rowCell.setValue("some updated value")
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM