物业服务的工作原理是什么? [英] How exactly does Properties Service work?

查看:53
本文介绍了物业服务的工作原理是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我以前做的是当我使函数调用它们时,我将使变量具有相同的值,即var sheet = SpreadsheetApp.openById('id').getActiveSheet();,有人建议/要求我将工作表另存为命名属性并继续调用在函数中使用它,而不是像100个一样具有完全相同的值的相同变量.

So what I did previously is when I make functions to call them, I would make variables of the same value, which is var sheet = SpreadsheetApp.openById('id').getActiveSheet();, and someone suggested/required me to save the sheet as a named property instead and keep calling it in the functions instead of making like 100 of same variables with the exact same values.

我在YouTube和类似内容上观看了视频-试试了:

I watched the videos on Youtube and stuff - tried this out:

var documentProperties = PropertiesService.getDocumentProperties();
var sheetID = "1dRAXZRHMgfR_oX1ZF1RFeonUi7ZPk_wbRf7Jx0UvCjE";
documentProperties.setProperties('spreadsheetID', sheetID);
var sheet = SpreadsheetApp.openById(documentProperties.getProperty(spreadsheetID)).getActiveSheet();
var logsheet = SpreadsheetApp.openById(documentProperties.getProperty(spreadsheetID)).getSheetByName("Log");
documentProperties.setProperties('timesheet', sheet);
documentProperties.setProperties('logsheet',log);

所以我需要能够在所有其他函数中调用变量工作表和日志工作表-如果我在函数onOpen()中拥有这些工作表,那么仅使用documentProperties.getProperties(timesheet)documentProperties(logsheet)就可以做到这一点吗?

So I need to be able to call the variables sheet and logsheet in all other functions - if I have these at function onOpen(), will I be able to do that by just simply using documentProperties.getProperties(timesheet) or documentProperties(logsheet)?

推荐答案

简短的回答是否定的,您不能将它们作为函数documentProperties(logsheet)调用.正确的方法是documentProperties.getProperty('logsheet').

Short answer is no, you can't call them as a function documentProperties(logsheet). The correct way is documentProperties.getProperty('logsheet').

您可以将它们用作某种全局变量,但是使用的方法不正确:

You can use them as some kind of global variables, but you are using the wrong methods:

  var documentProperties = PropertiesService.getDocumentProperties();
  var spreadsheetID = '1dRAXZRHMgfR_oX1ZF1RFeonUi7ZPk_wbRf7Jx0UvCjE'
  documentProperties.setProperty('spreadsheetID', spreadsheetID);

  var sheet = SpreadsheetApp.openById(documentProperties.getProperty('spreadsheetID')).getActiveSheet();
  var logsheet = SpreadsheetApp.openById(documentProperties.getProperty('spreadsheetID')).getSheetByName("Sheet1");

  documentProperties.setProperty('timesheet', sheet);
  documentProperties.setProperty('logsheet',log);

SetProperties(),以复数形式,是要设置多个属性:

SetProperties(), in plural, is to set multiple properties:

 documentProperties.setProperties{timesheet: sheet, logsheet: log};

请记住,不能在脚本之间共享属性,因此,您只能在同一项目的脚本中使用它们,但是可以,您可以在onOpen()函数中将它们用作任何常规变量.为确保获得所需的值,请在代码末尾添加Logger.log(documentProperties.getProperties()).

Keep in mind that Properties cannot be shared between scripts, so you can only use those within the scripts of the same project, but yes, you can use them in an onOpen() function as any regular variable. To make sure you are getting the values you want, put a Logger.log(documentProperties.getProperties()) at the end of the code.

这篇关于物业服务的工作原理是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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