I created this function to export Chromes device list through a Google Admin portal. It collects devices and filters by PRE_PROVIONED status. However, when trying to run all pages (130,000 devices) the error "Out of memory error." is being returned. How to fix this error? If I remove the pagination (nextPageToken), it works normally.
function exportChromebooksToSheet() {
// Adicione os escopos necessários
const maxResults = 50; // ajuste conforme necessário
let pageToken;
const allDevices = [];
do{
const options = {
orderBy: 'serialNumber',
maxResults: maxResults,
pageToken: pageToken
};
// Obtenha a instância do serviço de Administração
const response = (AdminDirectory.Chromeosdevices.list("my_customer", options));
let devices = response.chromeosdevices;
// Adicione os dispositivos da página atual à lista completa
allDevices.push(...devices);
// Atualize o token de página para a próxima página
pageToken = response.nextPageToken;
} while (pageToken);
// Filtrar dispositivos com status PRE_PROVISIONED
const preProvisionedDevices = devices.filter(function(device) {
return device.status === 'PRE_PROVISIONED';
});
writeToSheet(preProvisionedDevices);
}
function writeToSheet(devices) {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
// Limpe o conteúdo atual da planilha
sheet.clear();
// Escreva os cabeçalhos
var headers = ['Serial Number', 'Status'] // Adicione mais campos conforme necessário
sheet.appendRow(headers);
// Escreva os dados dos dispositivos
devices.forEach(function (device) {
var rowData = [
device.serialNumber,
device.status,
// Adicione mais campos conforme necessário
];
sheet.appendRow(rowData);
});
}
I tried setting maxResults with different parameters and nothing happened. If I remove the pageToken and put maxResults with any value, it always returns 42
I am by no means an expert in Apps Script, but here is how I solve this issue in my Org.
I do want to first mention that if automation is not your goal, a vendor's sheet plugin called Gopher can do what you asked, just not automatically.
Now for automation, that adds some complexity. That error is occurring because your array "allDevices" is holding so many devices that the Apps Script runtime can't handle it. It's the array's size that causes your error. The solution is to periodically dump a smaller chunk of those devices into something else, like Google Sheets or Google Drive.
Now, a secondary issue you'll have is that since you have over ~100,000 devices, you will also eventually get a timeout error. Apps Script can only run for 30 minutes, and if you notice, every time you run "AdminDirectory.Chromeosdevices.list", it takes around 2 seconds and only returns 100 devices at most. That means that for 30 minutes, the theoretical maximum devices you can get is 90,000. In practice, I often get less than that.
That means that to solve your problem, you will need to run the script multiple times, and also save your results in multiple smaller chunks. Shown below is some heavily commented psuedo code that stores the page token in your code, allowing it to run multiple times. In my case, I get around 32,000 results per script run, so I hardcode my Apps Script to run 3 times every night. In your case, you'll make as many executions as you need to make it work in Sheets.
The above code shows the changes you'll need to make to solve your error issue. I won't go over storing it in Sheets, since that is relatively easy to do comparatively and isn't the crux of the question, but I do want to let you know that you can call a filter for ChromeOSDevice.list like shown below:
That filter will let you filter on the Provision Statuses provided by google.
Lastly, the main reason I don't have example code for Sheets is that I stored it in JSON for my org, rather than sheets. Just incase it helps, below is my fully operation code that stores JSON files rather than sheets. I then use Power BI to combine the JSON, but any ETL tool would work: