简体   繁体   English

Google脚本外部电子表格数据验证

[英]Google Scripts external spreadsheet data validation

I was interested in automating data validation of a Google Spreadsheet against another Google Spreadsheet using Google Scripts. 我对使用Google脚本针对另一个Google Spreadsheet自动对一个Google Spreadsheet进行数据验证进行了研究。 I can't seem to find a way to reference the range of another spreadsheet using the openById method. 我似乎找不到使用openById方法引用另一个电子表格范围的方法。 Any thoughts? 有什么想法吗?

 function externalSheetDataValidation() { var cell = SpreadsheetApp.getActiveRange(); //var dataValidationSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("dataValidationRules"); //var dataValidationSheet = SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/d/10Z2s1bSRIhZzBBrMfPmhEphnPx-kJdV3LLlbv0L59g8/edit#gid=0"); var dataValidationSheet = SpreadsheetApp.openById("10Z2s1bSRIhZzBBrMfPmhEphnPx-kJdV3LLlbv0L59g8"); var sheet = dataValidationSheet.getSheets()[0]; var range = SpreadsheetApp.getActiveSheet().getRange("A3:A4"); var rule = SpreadsheetApp.newDataValidation() .requireValueInRange(range, true) .setAllowInvalid(false) .build(); cell.setDataValidation(rule); Logger.log(dataValidationSheet.getName()); } 

If I am understanding correctly, the issue you are having is selecting a data range from an external spreadsheet? 如果我理解正确,那么您遇到的问题是从外部电子表格中选择数据范围? Im assuming dataValidationSheet is supposed to be the external sheet. 我假设dataValidationSheet应该是外部工作表。 It looks like you were on the right track. 看来您在正确的轨道上。 It looks like the issue is that you are expecting the sheet to be active once you grab it. 看来问题在于,您希望工作表一旦被抓住就可以被激活。 Try this change: var range = sheet.getRange("A3:A4"); 尝试以下更改: var range = sheet.getRange("A3:A4");
Currently you are grabbing the external sheet, but then getting the range from the sheet that was already active. 当前,您正在抓取外部工作​​表,但是从已经处于活动状态的工作表中获取范围。 Hopefully this helps! 希望这会有所帮助!

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

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