我正在苦苦思索如何表述這個(可能是相當基本的)功能,盡管我相信我的用例可能不同,足以證明我的要求是正確的 - 如果不是,請隨時糾正我或為我指出適當的資源。
我的目標是遍歷 Sheet1 上 B36:B38 區域中的每個單元格,并且對于該區域中非空白的每個單元格,呼叫一個單獨的函式并將單元格值傳遞給它。理想情況下,這將使函式對該范圍內每個有值的單元格運行--如果第一個和第三個單元格有值,但第二個單元格沒有值,第一個單元格的值將被傳遞給函式,函式將運行,當該函式完成后,第三個單元格的值將被傳遞給函式,函式將再次運行。我覺得我已經接近下面的代碼了,但我不確定我哪里出了問題。任何見解都是有幫助的。
var spreadsheet = SpreadsheetApp.getActive()。
var searchRange = spreadsheet.getSheetByName('Sheet1')。 getRange('B36:B38').getValues() 。
for (i=0; i < searchRange.length; i ) {
if (searchRange.values !== null) {
var variableToPassOn = "testing" searchRange[0] 。
runFunction(variableToPassOn)
}
}
uj5u.com熱心網友回復:
從表單中呼叫和到表單中呼叫都很耗時,因此在腳本中存盤所有的值,然后在一次呼叫中推送到表單中會更快。作為參考,深入研究JavaScript陣列方法將是明智之舉。
function setValueToEmptyCell(){
const ss = SpreadsheetApp.getActiveSpreadsheet()。
const sheet = ss.getSheetByName('Sheet1'/span>)。
const valuesFlat = sheet.getRange('B36:B38')。 getValues().flat()。
//Empty array to store the output values;。
const output = [];
//對于每一行,我們檢查它是否為空白,如果為真,則將測驗值推送到輸出陣列中,如果不是,則推送原始值。
valuesFlat.forEach(row => {
if(row == ''/span>){
output.push(['Testing2']) 。
} else {
output.push([row])
}
})
//Because we have stored the original value we overwrite the hole range with the 'new' values.
sheet.getRange('B36:B38').setValues(output)。
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/319356.html
標籤:
