I am using the following code to make a copy of a file:
var backupFolder = DriveApp.getFolderById('1vOnO5aSOSFNWtOLHuAz4Lv2lsRgBwBpS');
var file = DriveApp.getFileById('1TJ_5Khw7wcqlIfbZ_-KLOQ-DsU02FsoRFYR1A3B4qAs');
file.makeCopy(file.getName(), backupFolder);
Most of the time that works without any issue.
Some times I get the following error:
We're sorry, a server error occurred. Please wait a bit and try again
Does anyone know how I can get a more meaningful error message to understand what is the issue?
To build on Anton's and Nancy's answers one useful approach I have found for error logging and catching important issues, is to append the error to a google sheet for reference with a timestamp and to send me a warning email with the failure reason. This gives me an instance notice something is not right, and helps me to keep track of issues.
function someImportantProcess(){
var ss = SpreadsheetApp.getActiveSpreadSheet();
var sheet = ss.getSpreadsheetByName('Sheet1');
try {
someImportant function(){
Logger.log('important stuff')
}
} catch(e){
sheet.appendRow([timestamp, e])
sendEmail(e)
}
}
sendEmail(e){
var subject = '[Important a script failed]';
var body = e;
sendEmail(myemail#google.com, subject, body);
}
Related
I have this script running from one source file to several other files where I use the data. The script has always tended to work fine but recently started getting the error
Exception: Service Spreadsheets timed out while accessing document with id.
I only get the error when the script is running for a particular destination file, as opposed to others. It also happens that this destination file is much larger than the others (maybe the problem). Anyway, the script is below and wondering if anyone has any alternative suggestions or feedback.
function Function_name() {
var ss=SpreadsheetApp.getActiveSpreadsheet()
var sourcesheet=ss.getSheetByName("RAWFINEX")
var data=sourcesheet.getDataRange().getValues()
var destinationfile=SpreadsheetApp.openById("Sheet ID")
var destinationsheet=destinationfile.getSheetByName("RAWFINEX")
var destinationlr=lastUsableRow(destinationsheet)
var destinationlc=destinationsheet.getLastColumn
var existingdata=destinationsheet.getRange(1,1,destinationlr,5).clearContent
var destinationrange=destinationsheet.getRange(1,1,data.length,data[0].length)
var pastedata=destinationrange.setValues(data)
}
Copy Data And Log
function CopyDataAndLog() {
var log = DriveApp.getFileById("logid");
var ss=SpreadsheetApp.getActive();
var ssh=ss.getSheetByName("RAWFINEX");
var svs=ssh.getDataRange().getValues();
var ss=SpreadsheetApp.openById("Sheet ID")
var fi=DriveApp.getFileById(ss.getId())
log.setContent(log.getBlob().getDataAsString() + `${fi.getSize()},${file.getName()},${file.getId()},${file.getType()}${'\r\n'}}`;
var tsh=ss.getSheetByName("RAWFINEX")
var tlr=lastUsableRow(tsh)
tsh.getRange(1,1,tlr,5).clearContent();
tsh.getRange(1,1,svs.length,svs[0].length).trg.setValues(svs);
}
This is surely a newbie question but I am breaking my head over it. I just don't understand what I'm doing wrong!
I have the following script which is incomplete and definitely incorrect. But my issue is that when I run it, I get the following error:
TypeError: SpreadsheetApp.getSpreadsheetById is not a function
This is the script:
function matchRow() {
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var ts = SpreadsheetApp.getSpreadsheetById('1Sq5prZI4a_UwesPTUQLnmXvtpJClyMcMlHM-m3JrE6g').getSheetByName('Quiz advertiser');
var countryIndex = ('Spain', 'Germany', 'France');
var getLastRowByMatch = matchRow();
if (ss.countryIndex = 'Spain');
getss.LastRowByMatch;
}
What am I doing wrong?
SpreadsheetApp.getSpreadsheetById doesn't exist. Instead of
var ts = SpreadsheetApp.getSpreadsheetById('1Sq5prZI4a_UwesPTUQLnmXvtpJClyMcMlHM-m3JrE6g').getSheetByName('Quiz advertiser');
use
var ts = SpreadsheetApp.openById('1Sq5prZI4a_UwesPTUQLnmXvtpJClyMcMlHM-m3JrE6g').getSheetByName('Quiz advertiser');
By the way, the lines after the referred one doesn't make any sense. It looks that you have to spend some time reviewing https://developers.google.com/apps-script/guides/sheets.
References
https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet-app
First time using script this is what I have after a couple hours.
I'm setting up a daily text blast which in testing works but I get this error Exception: Failed to send email: no recipient
at sendtext(Code:8:9)
Here's my sheet:
https://i.stack.imgur.com/RS5c2.png
And my code:
function sendtext() {
var sh=SpreadsheetApp.getActive().getSheetByName("Word-a-Day");
var lrow=sh.getLastRow()
for(var i=2;i<=lrow;i++) {
MailApp.sendEmail(sh.getRange(i,2).getValue(), "Khmer Word-A-Day", sh.getRange(2, 3).getValue())
}
}
The test texts goes through but I get the an error every time. Is this because of using the sms gateway?
Try this:
function sendtext() {
var ss=SpreadsheetApp.getActive();
var sh=ss.getSheetByName("Word-a-Day");
var lrow=sh.getLastRow();
var rg=sh.getRange(2,2,sh.getLastRow()-1,2);
var vs=rg.getValues();
var q=MailApp.getRemainingDailyQuota();
for(var i=0;i<vs.length;i++) {
MailApp.sendEmail(vs[i][0], "Khmer Word-A-Day", vs[i][1]);
//Utilities.sleep(5000);//Perhaps you need a delay here.
}
}
Found random space in a cell in the text gateway column so it was throwing an error. Thanks for the help.
Recently some of my app scripts are showing errors when executed simultaneously. Everything worked fine for years, apparently the change happened on Google side.
This is the execution transcript result:
Unfortunately, the error does not show file name and line number to debug it properly, but my first line is:
var ss = SpreadsheetApp.getActiveSpreadsheet()
Therefore I assume that calling SpreadsheetApp simultaneously by instances of the app is causing the error. SpreadsheetApp is not in the loop, nor inside any function. It is called to define global variable ss to be used by the app.
My apps are executed under my user name, since users should not have access to the Spreadsheet I'm accessing. I'm afraid I can't keep the entire Spreadsheet in Cache Service - I want app to update the Spreadsheet when it it called.
UPDATE:
Here is some relevant code from one of the apps.
function doGet(e){
var ref = e.parameter.ref;
var ss = SpreadsheetApp.openById('########')
var logsheet = ss.getSheetByName('LOG');
logsheet.appendRow([new Date(),ref,JSON.stringify(e.parameter)])
try{
//Main code goes here
return ContentService.createTextOutput(JSON.stringify({result:"ok"})).setMimeType(ContentService.MimeType.JSON);
}catch(e){
return ContentService.createTextOutput(JSON.stringify(e)).setMimeType(ContentService.MimeType.JSON)}
}
As you can see I have try {} catch(e) in my function, and since the error I'm getting is not caught by it, I assume it happens right before.
Just discovered manual hack to avoid this error:
Open your spreadsheet from private browser mode. This will increase number of document viewers and will increase limits :)
You can put everything in the try-catch block. Also remember to publish a new version of the web app before execution.
function doGet(e) {
var response = {};
try {
SpreadsheetApp.openById('########')
.getSheetByName('LOG')
.appendRow([new Date(), e.parameter.ref, JSON.stringify(e.parameter)]);
response.result = "OK";
} catch (error) {
response.result = "Error: " + error.toString();
}
return ContentService
.createTextOutput(JSON.stringify(response))
.setMimeType(ContentService.MimeType.JSON);
}
When I attempt to run the following code (Output will be XML):
function doGet(e){
return getFilesHtml();
}
function getFilesHtml() {
//Create second to top level XML parent
var xml = XmlService.createElement('response');
try{
var files = DriveApp.getFilesByName('ANNOUNCEMENTS.doc');
//Iterate through files and push to an array
var filesarray = new Array();
while(files.hasNext()) {
filesarray.push(files.next());
}
var file = getNewestFile(filesarray);
//Create necessary XML elements and information
var file_content = XmlService.createElement('content').setText(file.getBlob().getDataAsString('UTF-8'));
xml.addContent(file_content);
}catch(error){
var stat_child = XmlService.createElement('status').setText('failure');
var message_child = XmlService.createElement('message').setText(error.message);
xml.addContent(stat_child);
xml.addContent(message_child);
}
//Prepare and respond with XML
var document = XmlService.createDocument(xml);
var xml2 = XmlService.getPrettyFormat().format(document);
//Return the XML
return ContentService.createTextOutput(xml2).setMimeType(ContentService.MimeType.XML);
}
function getNewestFile(array){
array.sort(function(a,b){
return new Date(b.getDateCreated()) - new Date(a.getDateCreated());
});
return array[0];
}
I get the following failure response:
<response>
<status>failure</status>
<message>
We're sorry, a server error occurred. Please wait a bit and try again.
</message>
</response>
My ultimate goal is to get the contents of this file. I've tried .getAs('application/pdf'), but that works not, like this. Is there any other method or a method to correct this issue?
The error message "We're sorry, a server error occurred. Please wait a bit and try again." indicates that something went wrong deep within Apps Script. You'll have to troubleshoot the problem further before you can get help, and the Apps Script troubleshooting guide should help you do that.