Triggered functions and IMPORTRANGE

江枫思渺然 提交于 2019-12-04 12:48:34

The problem is with how functions like importRange works. Actually, they require the same access/permissions as the user account logged in using them, to avoid security holes (imagine an editor of a spreadsheet you own accessing other spreadsheets you have not shared with them).

Because of this (well, at least that's what I think), these formulas are only evaluated when you have the spreadsheet opened. When you close it and leave the script to run on a trigger, it will not find any result on the expected cells.

An easy workaround for this is to fetch the data using the script itself and quit using importRange. It's very easy to do, e.g.

var values = SpreadsheetApp.openById('external-spreadsheet-key').
  getSheetByName('Sheet1').getRange('A1:D100').getValues();
SpreadsheetApp.getActive().getSheetByName('DestSheet').
  getRange(1,1,values.length,values[0].length).setValues(values);

Obviously that the spreadsheet key, sheet's names and ranges are just an example here.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!