我正在使用代碼從多個作業表中提取資料。代碼運行良好,但我需要從多個位置提取資料。我想一次把它全部拉出來。下面的代碼可以很好地提取 ImportFALLOBS 資料(第一個函式),但不會執行 ImportSPRINGOBS 函式。
const ids = [
'1-PzUz2dlsLwA7lcndyWUZk4olgccE31jje8_JakZxXQ'
]
function ImportFALLOBS() {
let result = []
ids.forEach((id, i) => {
let [headers, ...data] = SpreadsheetApp.openById(id).getSheetByName('FALL OBS').getRange(5,1,1,13).getValues()
if (i == 0) { result.push(headers.flat()) }
data.forEach(r => result.push(r))
})
var master = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('FALLOBSIMPORT')
master.clearContents()
master.getRange(2, 1, result.length, result[0].length).setValues(result)
}
function ImportSPRINGOBS() {
let result = []
ids.forEach((id, i) => {
let [headers, ...data] = SpreadsheetApp.openById(id).getSheetByName('SPRING OBS').getRange(5,1,1,13).getValues()
if (i == 0) { result.push(headers.flat()) }
data.forEach(r => result.push(r))
})
var master = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('SPRINGOBSIMPORT')
master.clearContents()
master.getRange(2, 1, result.length, result[0].length).setValues(result)
}
uj5u.com熱心網友回復:
您可以通過組合通用部分來使代碼更小更快,只需更改作業表名稱:
function ImportOBS() {
const sheetNames = ['FALL OBS','SPRING OBS'];
const importNames = ['FALLOBSIMPORT', 'SPRINGOBSIMPORT'];
sheetNames.forEach((sheetName,i)=>{
let result = []
ids.forEach((id, i) => {
let [headers, ...data] = SpreadsheetApp.openById(id).getSheetByName(sheetName).getRange(5,1,1,13).getValues()
if (i == 0) { result.push(headers.flat()) }
data.forEach(r => result.push(r))
})
var master = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(importNames[i])
master.clearContents()
master.getRange(2, 1, result.length, result[0].length).setValues(result);
})
}
現在你只有一個功能:ImportOBS
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/468283.html
上一篇:如果提示為空,如何停止js腳本
