在同一功能中创建和发送工作表 [英] Creating and Sending sheets in the same function

查看:30
本文介绍了在同一功能中创建和发送工作表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个简单的脚本,其中包含创建新工作表的功能和通过电子邮件发送这些工作表的主要功能.当我尝试调用该函数在主函数内创建工作表时,它会通过电子邮件发送空工作表.这是我正在做的示例:

I am trying to create a simple script with a function to create new sheets and a main function to email those sheets. When I try to call the function to create sheets inside the main function, it emails empty sheets. Here is a sample of what I am doing:

function createnewsheets(mainfile, newsheetname) {
  mainfile.insertSheet(newsheetname);
  var sheet = mainfile.getSheetByName(newsheetname);
  var cell = sheet.getRange("A1");
  cell.setValue("Hello");
  return sheet;   
}
function doall(){
  var mainfile = SpreadsheetApp.openByUrl('https://docs.google.com/...');
  var sheet = createnewsheets(mainfile, "random");
  ////code to send sheet as attachment in email///
}

完整代码在这里.

现在这会发送空工作表,但如果首先执行 createnewsheets 而不返回并获取名称为 doeverything 的工作表,它确实可以工作.

Now this sends empty sheet, but if first execute createnewsheets without returning and fetch the sheet with the name in doeverything, it does work.

我认为这可能是创建工作表后等待的问题,所以我也尝试了 Utilities.sleep() 但这并没有改变任何东西.

I thought it might be an issue of wait after creating a sheet, so I also tried Utilities.sleep() but that didn't change anything.

知道如何改进会很有帮助.提前致谢.

It would be very helpful to know how can I improve. Thanks in advance.

推荐答案

Google Apps Script 应用对 Google Sheet 所做的更改 分批:

Google Apps Script apply changes made on Google Sheet in batches:

脚本通常需要从电子表格中读入数据,执行计算,然后将数据的结果写到电子表格中.Google Apps Script 已经有一些内置优化,例如使用前瞻缓存来检索脚本可能获得的内容,并编写缓存以保存可能设置的内容.

Scripts commonly need to read in data from a spreadsheet, perform calculations, and then write out the results of the data to a spreadsheet. Google Apps Script already has some built-in optimization, such as using look-ahead caching to retrieve what a script is likely to get and write caching to save what is likely to be set.

使用 SpreadsheetApp.flush() 在通过电子邮件发送工作表之前强制"应用您的脚本所做的更改.

Use SpreadsheetApp.flush() to "force" the application of the changes that your script made before sending the sheets by email.

相关

这篇关于在同一功能中创建和发送工作表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆