简体   繁体   中英

Google Scripts external spreadsheet data validation

I was interested in automating data validation of a Google Spreadsheet against another Google Spreadsheet using Google Scripts. I can't seem to find a way to reference the range of another spreadsheet using the openById method. 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. 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");
Currently you are grabbing the external sheet, but then getting the range from the sheet that was already active. Hopefully this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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