我正在嘗試:
讓谷歌腳本執行一個函式,在本例中基于 F 列將公式(我認為使用 R1C1)插入 G 列,并在公式中使用變數作為列參考。公式為 =NETWORKDAYS。我想確保我的函式搜索列標題名稱而不是數字,以防列被移動。
插入到 G 列中的公式會改變它從哪一列中提取,具體取決于 F 列
對于我們這里的示例,如果在 F 列中為 Yes,則 G 列將具有公式 =NETWORKDAYS(A2,D2),并將其分別輸入到 G 列中的每個單元格。
如果不是,則公式為 =NETWORKDAYS(A2,B2),公式分別插入到 G 列的每個單元格中。
當前的問題:
- 我不確定如何對此進行編碼,以便公式使用列標題名稱而不是硬編碼的列號參考,就像您在 R1C1 表示法中所做的那樣
- 我不擅長 IF 陳述句,并且“通過”范圍內的專案(即使函式通過范圍),這對我來說仍然是一個灰色區域
當前作業表:

我想發生什么/腳本的最終結果:

是 公式示例

無公式示例

當前代碼:
function trainingDays(){
//const/variables to find Training Days column
const ss = SpreadsheetApp.getActiveSpreadsheet();
const ws = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('sheet1');
const tf = ws.createTextFinder('Training Days');
tf.matchEntireCell(true).matchCase(false);//finds text "Training Days" exactly
const regionCellCol = tf.findNext().getColumn()//finds first instance of training days
const regionCellRow = tf.findNext().getRow()
//const/variables to find Race Date Announced
const tfRaceDateAnnounced = ws.createTextFinder('Race Date Announced');
tfRaceDateAnnounced.matchEntireCell(true).matchCase(false);//finds text "Race Date Announced" exactly
const rdaCellCol = tfRaceDateAnnounced.findNext().getColumn()//finds first instance of race date announced
const rdaCellRow = tfRaceDateAnnounced.findNext().getRow()
//const/variables to find Training Date Ended
const tfTrainingDateEnded = ws.createTextFinder('Training Date Ended');
tfTrainingDateEnded.matchEntireCell(true).matchCase(false);//finds text Training Date Ended
const tdeCellCol = tfTrainingDateEnded.findNext().getColumn()//finds first instance of training date ended
const tdeCellRow = tfTrainingDateEnded.findNext().getRow()
//const/variables to find Training: Yes or No
const tfTrain = ws.createTextFinder('Training: Yes or No');
tfTrain.matchEntireCell(true).matchCase(false);//finds text Training: Yes or No
const trainCellCol = tfTrain.findNext().getColumn()//finds first instance of Training: Yes or No
const trainCellRow = tfTrain.findNext().getRow()
//const/variables to find Race Date Commenced
const tfRDC = ws.createTextFinder('Race Date Commenced');
tfRDC.matchEntireCell(true).matchCase(false);//finds text Race Date Commenced
const rdcCellCol = tfRDC.findNext().getColumn()//finds first instance of race date commenced
const rdcCellRow = tfRDC.findNext().getRow()
//variable formulas
var trainingDaysFormulaNo = [] //is =NETWORKDAYS(Race Date announced, race date commenced) ONLY IF Training is No
var trainingDaysFormulaYes = [] //is =NETWORKDAYS(race date announced, training date ended) ONLY IF Training is Yes
ws.getRange(regionCellRow 1,regionCellCol,ws.getLastRow(),1).setFormulaR1C1()//not sure if this would work if I can figure out the formula to put in the .setFormulaR1C1 if I could pull the variable formulas and put into this, as an example .setFormulaR1C1(trainingDaysFormulaNo)
}//end of function trainingDays
我認為我的代碼會做什么
我認為這段代碼可以讓我將列名范圍插入到 R1C1 公式中,并在單元格范圍中使用 setFormulaR1C1。此外,我不確定該函式要執行什么樣的 IF 陳述句才能正常作業。
我試過的:
- Reviewing some items on stackoverflow but it seems to only relate to changing A1 notation to R1C1 or is excel specific
- I was hoping to be smart/clever using the text find features to call to the columns and get ranges that way
References:

輸出 2(不同的列位置):

轉載請註明出處,本文鏈接:https://www.uj5u.com/net/325156.html
標籤:google-apps-script google-sheets
