每当我打开工作表时,函数都会重新计算,我只希望它在编辑时重新计算 [英] function recalculates every time i open the sheet, I only want it to recalculate on edit

查看:141
本文介绍了每当我打开工作表时,函数都会重新计算,我只希望它在编辑时重新计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

function timestamp() {
return new Date()
}

每次打开工作表时,该函数都会重新计算.我只希望它在编辑时重新计算. 我试图弄乱"onEdit",但我永远无法使其正常工作.我还尝试了当前项目触发器",但这似乎无法解决问题.为简单起见,我很抱歉,我根本无法找到或找出答案. Google表格脚本 https://docs.google.com/spreadsheets/d /1-Ix9LUmMYNqWnz0mB2PAg9ocJ-TwszqHRbqxAchcxhc/edit#gid = 0

The function recalculates every-time i open the sheet. i only want it to recalculate on edit. I have tried to mess with "onEdit" but i can never get it to work. I have also tried the "current project triggers" but that doesn't seem to fix the problem. I apologize that for the simplicity of the question, I simply can't find or figure out the answer. Google sheets script https://docs.google.com/spreadsheets/d/1-Ix9LUmMYNqWnz0mB2PAg9ocJ-TwszqHRbqxAchcxhc/edit#gid=0

推荐答案

(根据您对问题的评论)它看起来像您已经创建了一个自定义公式并将其放在单元格中一样.如果是这种情况(意味着您实际上在一个单元格中有= timestamp()),那么它将始终在打开和编辑时都重新计算.

It looks (based on your comments on the question), like you've created a custom formula and put it in a cell. If that's the case (meaning that you actually have =timestamp() in a cell), then it will always recalculate both on open and on edit.

这就是公式(标准公式和自定义公式)的工作方式.当电子表格中的公式开始过多时,您会注意到这一点,它开始变慢,甚至无法正确加载.

That's just how formulas (both the standard ones and custom ones) work. You'll notice this when you have start to have too many formulas in a spreadsheet and it starts to slow down, or even not load correctly.

如果您想要只在编辑时更新的内容,则必须编写一个完全可以执行此操作的函数,然后设置一个在编辑时调用它的触发器.

If you want something that only updates on edit, you'll have to write a function that does exactly that, then set a trigger that calls it on edit.

例如,此函数会将当前日期放在当前电子表格第一张纸的A1单元格中:

For example, this function will put the current date in cell A1 of the first sheet of the current spreadsheet:

function setTimestamp()
{
    var thisSS = SpreadsheetApp.getActiveSpreadsheet();
    var firstSheet = thisSS.getSheets()[0];
    var firstCell = firstSheet.getRange(1, 1);

    firstCell.setValue(new Date());
}

此类函数仅在您调用它时运行,因此您可以将触发器设置为所需的任何值.显然,您必须弄清楚如何选择要显示日期的单元格.

This kind of function will only run when you call it, so you can set the trigger to whatever you want. Obviously, you'll have to figure out how to select the cell where you want the date to appear.

这篇关于每当我打开工作表时,函数都会重新计算,我只希望它在编辑时重新计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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